I've a menu screen which must be updated before the login screen closed
Not at all - it's an XY problem. In other words: your design is wrong. You need to lightly couple the login screen somehow to the menu screen, so that the menu screen gets the information it needs to update itself, before the login screen is closed. At any point after that, the login screen can indeed close.
Most likely you're tightly coupling the login screen with login controller, and thus the LoginScreen
class should emit the event that the MenuScreen
will process.
Your current signal names suggest very tight coupling between the screens. There is just one signal that you need: loginDone(const LoginData &)
, where LoginData
is a structure/class that carries the information about the logged in user etc.
Then, the three lines of code from the question simply become:
auto d = this->getLoginData();
emit loginDone(d);
close();
and
LoginData LoginScreen::getLoginData() const {
LoginData d;
d.foo = this->foo();
d.bar = this->bar();
...
return d;
}
A function (ideally in a controller class) would then couple the LoginScreen
to MenuScreen
loosely via the LoginData
object:
void setLoginDataOnMenu(const LoginData &data, MenuScreen *menu) {
...
menu->show();
};
int main(int argc, char **argv) {
QApplication app{argc, argv};
LoginScreen login;
MenuScreen menu;
QObject::connect(&login, &LoginScreen::loginDone, &menu, [&](const LoginData &){
setLoginDataOnMenu(data, &menu);
});
login.show();
return app.exec();
};
Ideally, you'd want to have a separate controller class to implement the logic, instead of having it in screens. The LoginScreen
and MenuScreen
could then be views for the data exposed by the controller.