Results 1 to 6 of 6

Thread: QT4 scope problem

  1. #1
    Join Date
    Sep 2006
    Posts
    27
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question QT4 scope problem

    This is a very newbie question
    It' a basic problem of scope between objects.
    In attachment the full example code.

    I have the main object (qt4testMainWindow : public QMainWindow) and I have a secondary object (qt4testData: public QObject) created by the first one.

    The main object build a UI of QMainWindow type; the second handle data but have to comunicate with the main object: it must call a main object's method.

    So I've created the new secondary object passing it the reference to the main UI (see "main_window.cpp" file at line 30).
    It's all ok if I have to change a UI value (see "qt4test_data.cpp" at line 30) from inside the secondary object.

    My problem is: how can I invoke "qt4testMainWindow::changeVar" method from the
    secondary object?
    I tried the method you can see in "qt4test_data.cpp" at line 32 without success:

    Qt Code:
    1. main_ui->changeVar ( 20 );
    To copy to clipboard, switch view to plain text mode 

    but I have a compiler error:

    Qt Code:
    1. qt4test_data.cpp: In member function ‘void qt4testData::init()’:
    2. qt4test_data.cpp:32: error: base operand of ‘->’ has non-pointer type ‘Ui::MainWindow’
    To copy to clipboard, switch view to plain text mode 

    Any help?
    Thanks,
    the_bis
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT4 scope problem

    Either use a dot (".") instead of an arrow ("->") or redesign so that you an use signals and slots without having a reference to the main window object.

  3. #3
    Join Date
    Sep 2006
    Posts
    27
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question Re: QT4 scope problem

    Quote Originally Posted by wysota View Post
    Either use a dot (".") instead of an arrow ("->") or redesign so that you an use signals and slots without having a reference to the main window object.
    No luck

    Qt Code:
    1. qt4test_data.cpp: In member function ‘void qt4testData::init()’:
    2. qt4test_data.cpp:32: error: ‘class Ui::MainWindow’ has no member named ‘changeVar’
    To copy to clipboard, switch view to plain text mode 

    I'm becoming crazy...
    I tried first the solution you suggest... but was too difficult for me.

    I post now some sample code:

    "main_window.cpp":
    Qt Code:
    1. #include "main_window.h"
    2. #include "qt4test_data.h"
    3.  
    4. qt4testMainWindow::qt4testMainWindow (QWidget *parent) : QMainWindow (parent)
    5. {
    6.  
    7. ui.setupUi (this);
    8.  
    9. QObject::connect(ui.horizontalSlider, SIGNAL(valueChanged(int)), ui.spinBox, SLOT(setValue(int)));
    10. connect (ui.pushButton2, SIGNAL(pressed()),
    11. this, SLOT(slotPush2Pressed()));
    12.  
    13. init ();
    14.  
    15. }
    16.  
    17. void qt4testMainWindow::init ()
    18. {
    19. QString stringToSend = "TestString";
    20. int integerToSend = 10;
    21.  
    22. qt4testData *myqt4testData = new qt4testData ( stringToSend , integerToSend , ui );
    23.  
    24. statusBar()->showMessage (tr("Starting..."));
    25. }
    26.  
    27. void qt4testMainWindow::slotPush2Pressed ()
    28. {
    29. qDebug ("\nqt4testMainWindow::slotPush2Pressed\n");
    30. }
    31.  
    32. void qt4testMainWindow::changeVar ( int newValue )
    33. {
    34. qDebug ("\nqt4testMainWindow::changeVar - newValue: %d\n" , newValue );
    35. ui.spinBox->setValue ( newValue );
    36. }
    To copy to clipboard, switch view to plain text mode 


    "main_window.h":
    Qt Code:
    1. #include "ui_main_window.h"
    2.  
    3. #include <QtDebug>
    4.  
    5. class qt4testData;
    6.  
    7. class qt4testMainWindow : public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. qt4testMainWindow ( QWidget *parent = 0 );
    13. void changeVar ( int newValue );
    14.  
    15. signals:
    16.  
    17.  
    18. private:
    19. Ui::MainWindow ui;
    20. void init ();
    21.  
    22. public slots:
    23. void slotPush2Pressed ();
    24.  
    25. private slots:
    26.  
    27.  
    28. };
    To copy to clipboard, switch view to plain text mode 

    "qt4test_data.cpp":
    Qt Code:
    1. #include "qt4test_data.h"
    2.  
    3. qt4testData::qt4testData (QString& testString, int testNumber, Ui::MainWindow& main_ui_temp)
    4. {
    5. qDebug ("\nqt4testData::qt4testData -> constructor");
    6.  
    7. mytestString = testString;
    8. mytestNumber = testNumber;
    9.  
    10. qDebug () << "\nqt4testData::qt4testData -> 'mytestString' variable: " << mytestString << "\n";
    11. qDebug () << "\nqt4testData::qt4testData -> 'mytestNumber' variable: " << mytestNumber << "\n";
    12.  
    13. main_ui = main_ui_temp;
    14.  
    15. init ();
    16. }
    17.  
    18. void qt4testData::init ()
    19. {
    20. qDebug ("\nqt4testData::init -> init");
    21.  
    22. main_ui.spinBox->setValue (80);
    23.  
    24. main_ui.changeVar ( 20 );
    25. }
    To copy to clipboard, switch view to plain text mode 

    "qt4test_data.h":
    Qt Code:
    1. #include <QtDebug>
    2.  
    3. #include "ui_main_window.h"
    4.  
    5. class qt4testData: public QObject
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. qt4testData (QString& testString, int testNumber, Ui::MainWindow& main_ui_temp);
    11.  
    12. private:
    13. void init ();
    14.  
    15. Ui::MainWindow main_ui;
    16. QString mytestString;
    17. int mytestNumber;
    18.  
    19. };
    To copy to clipboard, switch view to plain text mode 

    Or can you tell me where to find documentation or an example to study?
    Thanks,
    the_bis

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT4 scope problem

    What is the type of main_ui? Where is it declared? You shouldn't do anything like:

    Qt Code:
    1. main_ui = main_ui_temp;
    To copy to clipboard, switch view to plain text mode 
    If you're passing a reference, then you have to treat it like one. The line above creates an unnecessary copy of the ui. Since ui is not a widget this is not forbidden, but still not advised.

    You're suffering from simple C++ errors which have nothing to do with Qt. I suggest you use signals and slots to interconnect the main form and that other object.

  5. #5
    Join Date
    Sep 2006
    Posts
    27
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Thumbs up Re: QT4 scope problem

    Quote Originally Posted by wysota View Post
    [...] I suggest you use signals and slots to interconnect the main form and that other object.
    Ok, thank you far your suggestion.
    I think this is the correct solution...
    It works

    Can you see if it is all ok?
    Thanks,
    the_bis

    "main_window.h":

    Qt Code:
    1. #include "ui_main_window.h"
    2. #include "qt4test_data.h"
    3.  
    4. #include <QtDebug>
    5.  
    6. class qt4testData;
    7.  
    8. class qt4testMainWindow : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. qt4testMainWindow ( QWidget *parent = 0 );
    14. void changeVar ( int newValue );
    15.  
    16. signals:
    17.  
    18.  
    19. private:
    20. void init ();
    21.  
    22. Ui::MainWindow ui;
    23. qt4testData * myqt4testData;
    24.  
    25. public slots:
    26. void slotPush2Pressed ();
    27. void mainSlot ( int newValue );
    28.  
    29. private slots:
    30.  
    31.  
    32. };
    To copy to clipboard, switch view to plain text mode 

    "main_window.cpp":

    Qt Code:
    1. #include "qt4test_data.h"
    2.  
    3. qt4testData::qt4testData ( QString& testString, int testNumber, QObject * parent )
    4. {
    5.  
    6. mytestString = testString;
    7. mytestNumber = testNumber;
    8.  
    9. qDebug () << "\nqt4testData::qt4testData -> 'mytestString' variable: " << mytestString << "\n";
    10. qDebug () << "\nqt4testData::qt4testData -> 'mytestNumber' variable: " << mytestNumber << "\n";
    11.  
    12. connect ( this, SIGNAL ( secondarySignal ( int ) ), parent, SLOT ( mainSlot ( int ) ) );
    13.  
    14. init ();
    15.  
    16. }
    17.  
    18. void qt4testData::init ()
    19. {
    20.  
    21. qDebug ("\nqt4testData::init\n");
    22.  
    23. emit ( secondarySignal ( 75 ) );
    24.  
    25. }
    26.  
    27.  
    28. void qt4testData::secondarySlot ()
    29. {
    30.  
    31. qDebug ("\nqt4testData::secondarySlot\n");
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

    "qt4test_data.h":

    Qt Code:
    1. #include <QtDebug>
    2.  
    3. class qt4testData: public QObject
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. qt4testData ( QString& testString, int testNumber, QObject * parent );
    9.  
    10. private:
    11. void init ();
    12.  
    13. QString mytestString;
    14. int mytestNumber;
    15.  
    16. signals:
    17. void secondarySignal ( int newValue );
    18.  
    19. public slots:
    20. void secondarySlot ();
    21.  
    22.  
    23. };
    To copy to clipboard, switch view to plain text mode 

    "qt4test_data.cpp":

    Qt Code:
    1. #include "qt4test_data.h"
    2.  
    3. qt4testData::qt4testData ( QString& testString, int testNumber, QObject * parent )
    4. {
    5.  
    6. mytestString = testString;
    7. mytestNumber = testNumber;
    8.  
    9. qDebug () << "\nqt4testData::qt4testData -> 'mytestString' variable: " << mytestString << "\n";
    10. qDebug () << "\nqt4testData::qt4testData -> 'mytestNumber' variable: " << mytestNumber << "\n";
    11.  
    12. connect ( this, SIGNAL ( secondarySignal ( int ) ), parent, SLOT ( mainSlot ( int ) ) );
    13.  
    14. init ();
    15.  
    16. }
    17.  
    18. void qt4testData::init ()
    19. {
    20.  
    21. qDebug ("\nqt4testData::init\n");
    22.  
    23. emit ( secondarySignal ( 75 ) );
    24.  
    25. }
    26.  
    27.  
    28. void qt4testData::secondarySlot ()
    29. {
    30.  
    31. qDebug ("\nqt4testData::secondarySlot\n");
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT4 scope problem

    If it works then it's ok.

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.