Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for PostMessage functionality in Qt

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?

like image 389
Jesse Stimpson Avatar asked Oct 14 '10 20:10

Jesse Stimpson


4 Answers

Check QCoreApplication::postEvent().

like image 150
Donotalo Avatar answered Oct 04 '22 01:10

Donotalo


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)

like image 42
jkerian Avatar answered Oct 04 '22 02:10

jkerian


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.

like image 25
Frank Osterfeld Avatar answered Oct 04 '22 00:10

Frank Osterfeld


All answers so far are good, I just want to add that you can also use connect() with Qt::QueuedConnection for the connection type.

like image 28
Ringding Avatar answered Oct 04 '22 01:10

Ringding