Stefano
Stefano

Reputation: 4031

QMetaObject::invokeMethod execution is postponed

I have a method of my class which calls QMetaObject::invokeMethod. From the documentation I read that using Qt::DirectConnection should invoke the slot immediately. On my code I seams to experience that the slot is called only at the end of the execution of my method. I have therefore tried to put a long sleep between the invokeMethod and the end of the function and I do see the slot being executed at the end of the sleep time.

void myTest(){
    QMetaObject::invokeMethod(obj, "MyMethod",
                              Qt::DirectConnection,
                              Q_ARG(QString, myString));
    for(int j=0;j<10;j++)
    {
            qDebug() << "j: "<< j;
            Sleep(1000);
    }
}

Any idea why invoke is waiting the end of the function? I have also tried using Qt::AutoConnection

Upvotes: 0

Views: 809

Answers (1)

Alberto
Alberto

Reputation: 56

In your example Sleep(1000); is blocking the event loop then the slot can not be triggered.

To make it work there are 2 possible solutions:

  1. Use a QTimer.
  2. Before each Sleep(1000); call QApplication::processEvents(). This call will process all the pending events.

For more info you can read this answer: https://stackoverflow.com/a/26552350/8644816

Upvotes: 2

Related Questions