Question

I have a thread created by inheriting QThread in which I called exec() to initiate the event loop. And this class that inherits QThread has a method in it.

How can I call that method from the main thread for it to execute in the child thread? I assume that the execution of that method has to be queued in the child thread's event loop, so calling threadObject->childThreadMethod() won't be a good idea. Is there any solution to this?

Was it helpful?

Solution

You can not call every member functions of the thread, but only slots and Q_INVOKABLE methods.

Use QMetaObject::invokeMethod() to call such a method, and make sure to set the connection type to Qt::QueuedConnection.

That way, the method will be called in the second thread whenever the control returns to the event loop in the second thread. The caller, i.e. the main thread, will continue immediately, so you won't be able to get a return value from the called method.

Behind the scenes, QMetaObject::invokeMethod adds a MetaCallEvent to the second thread's event queue.

Alternatively, create a signal/slot connection, and then emit a signal whenever you want the slot to be called in the other thread.

OTHER TIPS

to run some function in a separate thread you can use QtConcurrent::run (i use it with QFutureWatcher). To run it every 5 or so seconds, use QElapsedTimer class

QFuture<void> future = QtConcurrent::run(this, &MyClass::foo2, ...foo2 arguments);

http://qt-project.org/doc/qt-4.8/qtconcurrentrun.html#run or check it here https://stackoverflow.com/search?q=QtConcurrent%3A%3Arun

or you can subclass QThread, reimplement QThread::run() method with the stuff you want to happen in your thread, and then create an instance of your thread and call start() on it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top