Unsolved Boost::thread and Qt UI Thread
-
Hi,
I would like to know the possibilities of updating UI from boost:thread.
I knew about Qt's Thread and UI thread connectivity.
The boost:thread is started from QWidget::somefunction().
QWidget::somefunction()
{
boost::thread mythread(&MainView::callInitialize, this);
}
voidMainView::callInitialize()
{
PluginFactory::getInstance().initialize();
signal(); // this will emit the signal to the callback (threadended)available in MainView.
}
void :MainView::threadEnded()
{
std::cout << QThread::currentThread() << std::endl;
splashView->hide();
delete splashView;
splashView = NULL;
sui->dialog->show();
}When "ui->dialog->show();" gets executed it crashes.
The reason i found is the boost::thread only executes the ui->dialog->show(); not the main thread.
I need the MainThread to execute ui->dialog->show(); ..
Please provide any suggesstion.
Thanks.
-
@inforathinam Have not tried it myself but I would try
@ui->dialog->moveToThread(*targetThreat);@ -
@artwaw That's absolutely not going to work. Widgets shouldn't be moved to other threads. All ui code needs to run in the main thread.
@inforathinam Qt's signal/slot connections do (by default) a runtime check at the time of signal emission. If both sender and the receiver live in the same thread then a direct connection is performed i.e. the slot is invoked in the same thread as the signal was emitted in (it's more or less equivalent to a direct function call). In your case that is exactly what's happening since sender and receiver are the same object (MainView). What you want though is to make a queued connection i.e. the slot to be invoked in the main thread, no matter which thread emitted the signal. Since your object is both sender and receiver Qt can't make that deduction automatically, so you need to help it with an extra argument to the
connect()
call:connect(mainViewInstance, &MainView::signal, mainViewInstance, &MainView::threadEnded, Qt::QueuedConnection);
This way the slot will be called in the receiver's thread (I'm assuming MainView instance is a QObject that was created in the main thread).