Question

What is the best way to communicate between two windows in Qt?

I need to have a separate login window and main application window which appears one after another (the second one, I mean main application window, will show only if the login was successful). Should I create these two objects (login window and main application objects) in the main function or make login window data member of the main application class and create it in the constructor of the main application class?

Was it helpful?

Solution

You can create login window as a data member of the main application class and create it in its constructor. Next you can invoke login by connecting a signal named login_ asked() of the main class to a slot named perform_login() and emitting the signal after that:

QObject::connect(this,SIGNAL(login_asked()),this,SLOT(perform_login())
                                                ,Qt::QueuedConnection);
emit login_asked();

You should hide your main window in the perform_login() slot and show your login form like:

this->setVisible(false);

loginfm->show();

You can notify your main application of the failure or success in login by signals and slots like:

QObject::connect(loginfm,SIGNAL(login_accepted()),this,SLOT(entered()));
QObject::connect(loginfm,SIGNAL(login_canceled()),this,SLOT(canceled()));

In the the slot entered() of your main window you should show the main window:

this->setVisible(true);

This way you can invoke login as many as you like during the application life cycle by just emiting the login_asked() signal.

OTHER TIPS

The login window should be a QDialog-derived window. This has accepted and rejected signals, which you can trivially handle in your main window. (This answers the question in the title)

It doesn't matter very much where you create them, that's really a style issue. I'd probably put it in main myself, but if I took over an existing codebase I wouldn't bother changing such details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top