PDA

View Full Version : doing a thing after the form is displayed



franco.amato
25th January 2011, 17:24
Good morning this is what I would implement:
just after that the main form is created and displayed I would connect to a database.
I tried to add the code to connect to the database in the ctor of the main application but it connect before the main form is displayed. How can I avoid this behavior?
Regards

Lykurg
25th January 2011, 17:43
Put the connection statements in an extra function and call it, after the main form was created/shown.

wysota
26th January 2011, 02:03
QMetaObject::invokeMethod(..., Qt::QueuedConnection) is also a good solution. It will run a slot only after starting the event loop.

ChrisW67
26th January 2011, 05:05
Or you can use a zero duration single-shot timer at the end of the constructor to have a similar effect:


MyClass::MyClass()
{
// stuff
QTimer::singleShot(0, this, SLOT(doConnect()));
}

// slot:
void doConnect()
{
// whatever
}