Boost Thread Qt Application
-
HI,all
I'm try to make as described in this link(with code):
http://wiki.qt.io/Boost_Thread_Qt_Application"So, let's call "Thread A" the thread that executes the public method of my library, that performs the three lines above. A new thread is created (the "boost" one), called "Thread B", that creates an instance of QApplication, my DialogProvider, and executes event loop of QApplication, everything in the same context. "Thread A" is blocked until I'm sure that my application is started, then returns.
Some time later, a new thread, called "Thread C", wants to call a different public method that shows my widget. I just need to call
app->showMyWidget(5,3.0);
Of course, the two parameters are specified as example. "Thread C" posts a custom event (that carries on all information I need to create my widget) to QApplication event loop, that forwards it to my DialogProvider customEvent method. In that method (I'm in QApplication context, i.e. "Thread B"), I can create my widget and show it in the end, I just signal (using a QWaitCondition) that my widget is shown, so "Thread C" can return."How can I make communication in this example with "Thread A" and "myWidget" in both direction?
-
Hi and welcome to devnet,
What kind of communication do you have in mind ? What will you pass between the two threads ?
-
How many times do you need to do that ?
-
What is the main thread exactly ?
-
@QTTman Does your main thread contain any QObjects? If so, you can use signals and slots to pass QStrings between the 2 threads.
Otherwise, your main thread can use
QMetaObject::invokeMethod()
withQt::BlockingQueuedConnection
to make a function run in your Qt thread and wait for a return value: http://doc.qt.io/qt-5/qmetaobject.html#invokeMethod -
@QTTman said in Boost Thread Qt Application:
If I'm right, I can't use any QObject in "main thread" before than QApp is initialized in the different thread.
Yes, you are right.
So, you can use
QMetaObject::invokeMethod()