Results 1 to 13 of 13

Thread: How to chain dialogs

  1. #1
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How to chain dialogs

    What I like to do is first show a dialog where the user can login to the database. On successfull connect/login this dialog shall be closed and a main window for the application shall be displayed. On error the login window shall stay open for retry, and by cancel button the application shoud terminate.

    Does anybody have or know about a sample how this is properly done, or at least give a snippet of the main() function demonstrating the chaining?

  2. #2
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: How to chain dialogs

    Something like this:

    Qt Code:
    1. #include <QApplication>
    2. #include "mylogindialog.h"
    3. #include "mymainwindow.h"
    4.  
    5. int main(int argc, char** argv) {
    6. QApplication app(argc, argv);
    7. MainWindow mw;
    8. LoginDialog ld;
    9. connect(&ld, SIGNAL(accepted()), &mw, SLOT(show()));
    10. ld.show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    It's nice to be important but it's more important to be nice.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to chain dialogs

    You might try something like this:
    Qt Code:
    1. int main( int argc, char **argv )
    2. {
    3. QApplication app( argc, argv );
    4.  
    5. // you need this if you use Qt 4.0.1:
    6. // app.setQuitOnLastWindowClosed( false );
    7.  
    8. PasswordDialog dlg;
    9. bool connected = false;
    10. do {
    11. if( dlg.exec() == QDialog::Accepted ) {
    12. // connect to database
    13. }
    14. else {
    15. return EXIT_FAILURE;
    16. }
    17. }
    18. while( ! connected );
    19.  
    20. MainWindow mw;
    21. mw.show();
    22.  
    23. // app.setQuitOnLastWindowClosed( true );
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to chain dialogs

    Thanks a lot!

  5. #5
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to chain dialogs

    I now have the main program working mostly as axeljaeger has suggested, with an additional connection to make the program quit after the last window is closed:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. ALoginView loginView;
    5. AMainView mainView;
    6. loginView.show();
    7. app.connect(&loginView, SIGNAL(accepted()), &mainView, SLOT(show()));
    8. app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    It works as expected as long as the login dialog is closed by clicking on the login button. However if I hit the enter key in a textedit instead, the main window is displayed very short, but then the program immediately terminates.

    Any ideas what could be the reason? Do I need to quit the program in a different way, or is quitting not necessary at all (such as in axeljaeger's example)?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to chain dialogs

    Quote Originally Posted by seneca
    app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
    You don't need this in Qt4.

    Quote Originally Posted by seneca
    the main window is displayed very short, but then the program immediately terminates.

    Any ideas what could be the reason?
    Do you happen to use Qt 4.0.1? If yes, read closely my previous post.

  7. #7
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to chain dialogs

    Actually I use Qt 4.1, is it only required for 4.0.1?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to chain dialogs

    Quote Originally Posted by seneca
    Actually I use Qt 4.1, is it only required for 4.0.1?
    There was a bug in Qt 4.0.1 and it ought to be fixed in Qt 4.1.

  9. #9
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to chain dialogs

    Qt Code:
    1. app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 
    Quote Originally Posted by jacek
    You don't need this in Qt4.
    After removing it, it became better but still from time to time the program quits when I confirm login with enter instead of clicking the button. I will therefore rewrite the main the way you suggested first, and do app.setQuitOnLastWindowClosed( false ); before the login dialog is displayed and enable it again before the main window is shown.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to chain dialogs

    Quote Originally Posted by seneca
    I will therefore rewrite the main the way you suggested first, and do app.setQuitOnLastWindowClosed( false ); before the login dialog is displayed and enable it again before the main window is shown.
    The only problem is that this trick won't work if you use a signal to show the main window.

  11. #11
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to chain dialogs

    Quote Originally Posted by jacek
    The only problem is that this trick won't work if you use a signal to show the main window.
    Yes, that's why I will rewrite it the way you suggested first, by *NOT* using a signal to chain.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to chain dialogs

    Quote Originally Posted by seneca
    Yes, that's why I will rewrite it the way you suggested first, by *NOT* using a signal to chain.
    Indeed... It looks like I need a rest.

  13. #13
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to chain dialogs

    OK, so here is the the main function which finally works stable now:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. ALoginView loginView;
    5. loginView.show();
    6. app.setQuitOnLastWindowClosed(false);
    7. if (loginView.exec()==QDialog::Accepted) {
    8. AMainView mainView;
    9. mainView.show();
    10. app.setQuitOnLastWindowClosed(true);
    11. return app.exec();
    12. } else
    13. return EXIT_FAILURE;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by seneca; 14th January 2006 at 23:09.

Similar Threads

  1. Replies: 4
    Last Post: 4th January 2009, 02:14
  2. Using a common datamodel across dialogs?
    By Frozenjim in forum Qt Programming
    Replies: 1
    Last Post: 14th October 2008, 01:14
  3. Closing all of the mainWindow's child dialogs
    By JPNaude in forum Qt Programming
    Replies: 4
    Last Post: 2nd October 2008, 14:18
  4. Round-corner dialogs?
    By WinchellChung in forum Newbie
    Replies: 2
    Last Post: 14th March 2008, 20:47
  5. how to program custom Dialogs with return values
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2006, 01:37

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.