Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Boost::thread and Qt UI Thread
Forum Updated to NodeBB v4.3 + New Features

Boost::thread and Qt UI Thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    inforathinam
    wrote on 5 Jul 2017, 16:34 last edited by
    #1

    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.

    A 1 Reply Last reply 5 Jul 2017, 16:39
    0
    • I inforathinam
      5 Jul 2017, 16:34

      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.

      A Offline
      A Offline
      artwaw
      wrote on 5 Jul 2017, 16:39 last edited by
      #2

      @inforathinam Have not tried it myself but I would try
      @ui->dialog->moveToThread(*targetThreat);@

      http://doc.qt.io/qt-5/qobject.html#moveToThread

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      -1
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 5 Jul 2017, 19:51 last edited by
        #3

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

        1 Reply Last reply
        4

        1/3

        5 Jul 2017, 16:34

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved