The Win32 API has a PostMessage
function that posts a message to the end of the GUI message queue to be processed later from the GUI thread, as opposed to SendMessage
which sends and processes the message synchronous with the calling thread.
Is there a Qt solution for PostMessage
functionality? A coworker suggested that Qt's server/socket implementation could provide it; is that a reasonable approach?
Check QCoreApplication::postEvent().
Look at QTimer::singleShot. In your case you'd want to use it with an msec
value of 0
, which should provide the same functionality. (This is regularly used to implement delayed intialization, until the GUI event loop is running)
Similar to the QTimer solution, but with the advantage that you can pass arguments, is the QMetaObject::invokeMethod way:
QString SomeClass::compute(const QString&, int, double);
...
QMetaObject::invokeMethod(obj, "compute", Qt::QueuedConnection,
Q_RETURN_ARG(QString, retVal),
Q_ARG(QString, "sqrt"),
Q_ARG(int, 42),
Q_ARG(double, 9.7));
The QueuedConnection queues it in the event loop, DirectConnection would execute it immediately.
All answers so far are good, I just want to add that you can also use connect()
with Qt::QueuedConnection
for the connection type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With