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. Is this correct, to have a thread calling slot in main process?

Is this correct, to have a thread calling slot in main process?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 538 Views
  • 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.
  • S Offline
    S Offline
    SRaD
    wrote on last edited by
    #1

    I was getting the following error in Qt5 because my MainWindow dropped a thread that was trying to update a treeview in the main thread.

    QObject: Cannot create children for a parent that is in a different thread.
    

    So in my thread class (derived from QObject) I have this function which takes the MainWindow object to which the slot will be called.

    void MyTest::set_model(MainWindow &mw)
    {
        connect(this, SIGNAL(update_tree), mw, SLOT(update_treeview),              
            Qt::QueuedConnection);
    }
    
    void MyTest::update_tree()
    {
        std::cout << "update_tree ........" << std::endl;
    }   
    

    And in my MainWindow, here is how I pass the MainWindow object to the thread so that it can emit a signal and call a slot in the main thread (TBD: emit signal).

    void MainWindow::update_treeview()
    {
        std::cout << "calling update_treeview() ..." << std::endl;
    }
    
    MyTest *test = new MyTest;
    test->set_model(this, tree_model, tree_view);
    
    std::thread t(test);
    t.join();
    

    It doesn't yet compile, but am I going about this correctly? My goal is to have the thread pass TreeModel data to the main process so the TreeView can be updated.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      If you want a model with thread support, you should check the implementation of QFilesystemModel.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • S Offline
        S Offline
        SRaD
        wrote on last edited by SRaD
        #3

        I have a custom model and am not using QFilesystemModel? Should the above work?

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          The short answer is that ther's a 99.9999% chance what you are doing is wrong.
          The model can't be read nor written from a non-gui thread if that model is connected to a view.
          The view calls methods from the model directly so any operation on the model from a different thread is a race condition.
          The correct way to do it is to emit a signal with the new data from the secondary thread and have the gui thread do the actual update

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          3
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            I didn't wrote you had to use QFileSystemModel but that you could get inspiration from it as its implementation uses thread and does it as @VRonin correctly describes it.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SRaD
              wrote on last edited by
              #6

              I don't think @VRonin got what I was saying in my OP, but what I explained in my OP sounds exactly like what VRonin explained, so I think I'm on the right track. Thanks both.

              1 Reply Last reply
              0

              • Login

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