Vitalii
Vitalii

Reputation: 4793

How to show dialog when main window became visible for the very first time in Qt widgets application?

I have Qt widgets application that has to authorize user with web service request before he can use the program. If user can not sign in for any reason, I have to exit the application.

.NET Framework implements Load event for that: when user sees window for the very firt time (the keyword here is "user sees"), it is possible to display dialog; if dialog result is not OK, we call close on main application window.

With Qt, we can override showEvent. However, it fires before user really can see main window. When I create dialog in showEvent, it appears without main window, and when I close main window in showEvent, it doesn't close (because it is not opened at the moment) and appears later event if user failed to sign in. It appears even after QApplication::quit() call.

My question is: is there a way to receive exactly the same functionality as in .NET Framework/Windows API and get event fired when user really sees window and not when he "may be sees something or will see soon"? It is possible to start timer from showEvent to get similar effect, but I think it is not professional, because you never know what may happend in user computer (may be its CPU works on 100% now), and you can not have real guarantee that timer will show dialog at correct moment.

Upvotes: 0

Views: 1803

Answers (3)

Mike
Mike

Reputation: 8355

I usually do something like this in my main.cpp:

int main(int argc, char* argv[])
{
    QApplication a(argc, argv);
    LoginDialog dialog;
    if(!dialog.exec()){
        return 1;
    }
    MainWindow w;
    w.show();
    return a.exec();
}

that (of course) after having LoginDialog defined to inherit from QDialog and MainWindow defined to inherit from QMainWindow.

Upvotes: 1

UmNyobe
UmNyobe

Reputation: 22890

Create the main dialog at application startup, and only create the main window when the service responds positively.

You don't really need to dig deep in the event handlers.

Upvotes: 0

Y.Nebesov
Y.Nebesov

Reputation: 31

I would try to create MainWindow and hide() it be default. The only widget to be shown at startup should be then the Login-Dialog. I would connect the successful login with the show() slot of the QMainWindow and the login failure - with the quit slot of the application.

Upvotes: 1

Related Questions