How will MainWindow get notified when in event loop.
-
Hi Im into Qt from MFC.
In MFC once you create a window the OnCreate(..) callback gets fired when we need to initialise things and start the app running.
But in Qt there's no way to capture when the application is ready and safely inside exec() - the event loop. Is there really a way to handle this in Qt ?thanx,
SeedOfLife -
In short: I think no.
But what do you want to handle there?
creation of the widgets (and their children) usually happens in the constructor (regardless whether the event loop is running or not). Filling in data to widgets can also be done there.What else is missing?
-
Right now my main() looks the typical >>
@int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();return a.exec();
}@
In the MainWindow constructor I want to initialise everything on the UI. After the a.exec() is called I am certain the app is initialised and in the event loop and want the login to spawn there. Basically my login must be handled once the app is in the event loop.
I tried using showEvent() but that gets called before the a.exec().
Tried with changeEvent() and catching ApplicationChange event but even they get fired before a.exec().So how do u guys suggest I let my little app know when its in the event loop. I can think of a one shot timer but that is not elegant and wont work at all times.
-
In our application we do it this way:
@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.slotLogin();return a.exec();
}
@In slotLogin I create a model dialog which runs its own event loop on exec().
Another, more Qt-ish, approach would be with a single shot QTimer:
@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QTimer::singleShot(0, &w, SLOT(slotLogin()));return a.exec();
}
@The timer times out immediately, but the event is processed only when the event loop has started.
-
[quote author="Franzk" date="1299070378"]Key to all this is that your login dialog won't be shown before you have a running event loop in the first place.[/quote]
Nope. The dialog is shown as soon as you call slot show() and dialog->exec() runs it's own event loop and works as expected. I can remove the app.exec() completely and the login works :-) You're right in sofar that the main window w will be fully functional only after app.exec().
-
[quote author="Franzk" date="1299079610"]Tested it: I'm not wrong:
@int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
//return a.exec();
while (true);
return 0;
}
@
Does nothing but waiting. Obviously w.exec() would work as it starts an event loop.[/quote]I never wrote that the above code works. The dialog is shown (in the sense of "made visible") - nothing more. What works is:
@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.exec();
return 0;
}
@ -
[quote author="Franzk" date="1299081352"]Different definition of showing then.[/quote]
Indeed - but nevermind. Calling show() without an exec() afterwards is stupid and only serves for corner case discussions :-) And I'm becoming more and more off topic...
Ahm, to get back to the question: I'd prefer the QTimer approach nowadays. This ensures that the even loop is already running - this might be important, depending on the workload in the dialog and other signals/events etc.
-
Hi guys, apologies for not having replied any sooner. Soution to this was to create a single shot timer. It ensured that I had a valid event loop by the time the signal was handled. As for the reason to wait for the event loop, well there were many. But the most prominent was for elegance. I could have put the code for the login dialog inside the MainWindow c'tor but that will look awkward. And besides once the login is handled the initial download should commence which is gonna need the event loop.
But what I found out in the process was that the one wudnt have to wait for the exec() call to see if
the events are being processed. exec() is merely an endless loop which waits for events and processes them. It's something which the code enters into when it dont have anything to do (like creating windows, setting properties, opening files etc.) and wait until the user quits. I hope this helps out anyone in the future.edit: I wanted to say, single shot timer is more elegant than waiting on show event. Show Events get called even before the exec() is called for the app. But the timer is only fired after exec() is called for.