Important: Please read the Qt Code of Conduct -

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().

    boost::thread mythread(&MainView::callInitialize, this);
    signal(); // this will emit the signal to the callback (threadended)available in MainView.
    void :MainView::threadEnded()
    std::cout << QThread::currentThread() << std::endl;
    delete splashView;
    splashView = NULL;

    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.


  • @inforathinam Have not tried it myself but I would try

  • Moderators

    @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).

Log in to reply