Results 1 to 10 of 10

Thread: Communication between MainWindow and a dialog

  1. #1
    Join Date
    Jul 2007
    Posts
    23
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Windows

    Question Communication between MainWindow and a dialog

    Hello again,

    I have a main window that opens a dialog when a button is pressed.
    When this happens it sends an instance of a class to the dialog for processing like so...

    Qt Code:
    1. void on_push_0_clicked()
    2. {
    3. KeyDialog dialog(this); //Create dialog
    4. dialog.setKey(zero); //use setKey function of dialog
    5. dialog.exec(); //Show dialog
    6. }
    To copy to clipboard, switch view to plain text mode 

    (Michiel helped me with that one)

    Now I want to send the processed instance back to the main window.

    Qt Code:
    1. void KeyDialog::on_buttonBox_accepted()
    2. {
    3. //Copy key for safety
    4. Key::Key K2 = K1; //K1 is a global instance that is processed earlier in the form
    5. //add new data from form
    6. //changes vars in K2
    7. mainWin.resetKey(K2); //sends K2 back to mainwindow
    8. }
    To copy to clipboard, switch view to plain text mode 

    But I guess my dialog has no knowledge of MainWindow because it says it hasn't been declared. How do I go about accesing this member function of MainWindow?

    Thank you all very much for your help,
    Backslash

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Communication between MainWindow and a dialog

    Since you create the dialog with the main window as parent, you could do a cast to get an instance of the window type:
    Qt Code:
    1. ((MainWindowClass*)parent())->resetKey(K2);
    To copy to clipboard, switch view to plain text mode 

    Regards

  3. The following user says thank you to marcel for this useful post:

    Backslash (2nd August 2007)

  4. #3
    Join Date
    Jul 2007
    Posts
    23
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Communication between MainWindow and a dialog

    I'm sorry, but I don't quite understand what doing a cast means.

    The MainWindow instance is called mainWin (from main.cpp)

    Qt Code:
    1. QApplication app(argc, argv);
    2. MainWindow mainWin;
    3. mainWin.show();
    4. return app.exec();
    To copy to clipboard, switch view to plain text mode 

    So do you mean for me to write:

    ((MainWindow*)parent())->resetKey(K2);

    or something else entirely?

    Thanks,
    Backslash

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Communication between MainWindow and a dialog

    Yes, the easiest way is to do that.
    In on_buttonBox_accepted, replace the last line with:
    Qt Code:
    1. ((MainWindow*)parent())->resetKey(K2);
    To copy to clipboard, switch view to plain text mode 

    Should work.

    To find out what a cast is, do a search on google on"c++ inheritance and polymorphism".

    Regards

  6. The following user says thank you to marcel for this useful post:

    Backslash (2nd August 2007)

  7. #5
    Join Date
    Jul 2007
    Posts
    23
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Communication between MainWindow and a dialog

    Thanks again for the help,

    When I use that line "((MainWindow*)parent())->resetKey(K2);" I get this error message from make:

    keydialog.cpp: In member function `void KeyDialog:n_buttonBox_accepted()':
    keydialog.cpp:117: error: `MainWindow' undeclared (first use this function)
    keydialog.cpp:117: error: (Each undeclared identifier is reported only once for
    each function it appears in.)
    keydialog.cpp:117: error: expected primary-expression before ')' token
    keydialog.cpp:117: error: expected `)' before "parent"
    mingw32-make[1]: *** [release\keydialog.o] Error 1

    Did I misunderstand something?

  8. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Communication between MainWindow and a dialog

    You have to include MainWindow.h in keydialog.cpp.

    A more elegant solution is to create a getter in the keydialog. This will return the key (K2).
    So, in the slot on_push_0_clicked, you can do:
    Qt Code:
    1. if(dialog.exec() == QDialog::Accepted )
    2. {
    3. Key::Key k = dialog.getKey2();
    4. resetKey(k);
    5. }
    To copy to clipboard, switch view to plain text mode 
    Regards

  9. The following user says thank you to marcel for this useful post:

    Backslash (2nd August 2007)

  10. #7
    Join Date
    Jul 2007
    Posts
    23
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Communication between MainWindow and a dialog

    Wow, that is a neat solution. Definitely going to try that one...

    Thanks for the patience and the insight,
    Backslash

  11. #8
    Join Date
    Jul 2007
    Posts
    23
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Communication between MainWindow and a dialog

    Is this the order in which I should put this?

    Qt Code:
    1. void MainWindow::on_push_0_clicked()
    2. {
    3. KeyDialog dialog(this);
    4. dialog.setKey(zero);
    5. dialog.exec();
    6. if(dialog.exec() == QDialog::Accepted )
    7. {
    8. Key::Key k = dialog.getKey();
    9. resetKey(k);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    In KeyDialog, My getKey(); looks like this:

    Qt Code:
    1. Key getKey()
    2. {
    3. return K2;
    4. }
    To copy to clipboard, switch view to plain text mode 

    (K2 is defined at the top of KeyDialog because I use on_buttonBox_accepted to take the current state of the form's widgets and translate those into values to be stored in K2)

    All of this seems ok to me but I'm getting a wierd error from make:

    release\mainwindow.o(.text+0x8455):mainwindow.cpp: undefined reference to `KeyDi
    alog::getKey()'
    release\mainwindow.o(.text+0x9915):mainwindow.cpp: undefined reference to `KeyDi
    alog::getKey()'
    release\mainwindow.o(.text+0xadd5):mainwindow.cpp: undefined reference to `KeyDi
    alog::getKey()'
    release\mainwindow.o(.text+0xc295):mainwindow.cpp: undefined reference to `KeyDi
    alog::getKey()'
    release\mainwindow.o(.text+0xd755):mainwindow.cpp: undefined reference to `KeyDi
    alog::getKey()'
    release\mainwindow.o(.text+0xec15):mainwindow.cpp: more undefined references to
    `KeyDialog::getKey()' follow
    collect2: ld returned 1 exit status

    I have no clue what that's about...

    Thanks again,
    Backslash

  12. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Communication between MainWindow and a dialog

    Well, this looks like c++ mistake:
    When you implement a function in a class, you have to prepend the name of the class and the resolution operator to it:
    Qt Code:
    1. Key::Key KeyDialog::getKey()
    2. {
    3. return K2;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Now, K2 has to be a member of the class in order to be visible in any of the class's member functions.

    Also, the correct thing to do is:
    Qt Code:
    1. void MainWindow::on_push_0_clicked()
    2. {
    3. KeyDialog dialog(this);
    4. dialog.setKey(zero);
    5. if(dialog.exec() == QDialog::Accepted )
    6. {
    7. Key::Key k = dialog.getKey();
    8. resetKey(k);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    exec(), by default, returns an exit code which shows you whether the user pressed ok or cancel( accepted or rejected).
    So you need to call exec only once.

    The compile errors tell you that you did not declared/defined the getKey function.
    To declare a function - means you add it in the class declaration, in the header:
    Qt Code:
    1. class KeyDialog : ...
    2. {
    3. public:
    4. Key::Key getKey();
    5. };
    To copy to clipboard, switch view to plain text mode 
    The function definition means the implementation. It appears that you have donh that( incorrectly although ).

    So, fix these errors and try again.

    Regards

  13. The following user says thank you to marcel for this useful post:

    Backslash (3rd August 2007)

  14. #10
    Join Date
    Jul 2007
    Posts
    23
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Communication between MainWindow and a dialog

    Thanks for all the help. I'm glad there's a newbie section of this forum.
    Now I just have to work out some more kinks in my code and implement a few more functions.

    Thanks again,
    Backslash

Similar Threads

  1. Replies: 2
    Last Post: 23rd May 2007, 04:51
  2. Opening a Dialog from a MainWindow FileMenu
    By nbkhwjm in forum Newbie
    Replies: 4
    Last Post: 17th April 2007, 13:24
  3. Replies: 3
    Last Post: 23rd July 2006, 19:02

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.