PDA

View Full Version : How to chain dialogs



seneca
12th January 2006, 16:07
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?

axeljaeger
12th January 2006, 16:13
Something like this:




#include <QApplication>
#include "mylogindialog.h"
#include "mymainwindow.h"

int main(int argc, char** argv) {
QApplication app(argc, argv);
MainWindow mw;
LoginDialog ld;
connect(&ld, SIGNAL(accepted()), &mw, SLOT(show()));
ld.show();
return app.exec();
}

jacek
12th January 2006, 16:16
You might try something like this:
int main( int argc, char **argv )
{
QApplication app( argc, argv );

// you need this if you use Qt 4.0.1:
// app.setQuitOnLastWindowClosed( false );

PasswordDialog dlg;
bool connected = false;
do {
if( dlg.exec() == QDialog::Accepted ) {
// connect to database
}
else {
return EXIT_FAILURE;
}
}
while( ! connected );

MainWindow mw;
mw.show();

// app.setQuitOnLastWindowClosed( true );
return app.exec();
}

seneca
12th January 2006, 16:45
Thanks a lot!

seneca
14th January 2006, 18:10
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:


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
ALoginView loginView;
AMainView mainView;
loginView.show();
app.connect(&loginView, SIGNAL(accepted()), &mainView, SLOT(show()));
app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
return app.exec();
}

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)?

jacek
14th January 2006, 20:32
app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
You don't need this in Qt4.


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.

seneca
14th January 2006, 20:42
Actually I use Qt 4.1, is it only required for 4.0.1?

jacek
14th January 2006, 20:46
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.

seneca
14th January 2006, 20:55
app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));

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.

jacek
14th January 2006, 21:19
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.

seneca
14th January 2006, 21:23
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.

jacek
14th January 2006, 21:49
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.

seneca
14th January 2006, 23:04
OK, so here is the the main function which finally works stable now:


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
ALoginView loginView;
loginView.show();
app.setQuitOnLastWindowClosed(false);
if (loginView.exec()==QDialog::Accepted) {
AMainView mainView;
mainView.show();
app.setQuitOnLastWindowClosed(true);
return app.exec();
} else
return EXIT_FAILURE;
}