Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved How to create a QComboBox in another thread?

    General and Desktop
    3
    6
    162
    Loading More Posts
    • 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.
    • M
      MasterBlade last edited by MasterBlade

      Hello I need to use a second thread for performance optimization. But soon I found that Qt only allows to create widgets in the main thread. I then emitted a signal to the main thread asking it to create a QComboBox but it still failed. App output the follow errors. What should I do?

      QObject: Cannot create children for a parent that is in a different thread.
      (Parent is EffectTable(0xdac7480f0), parent's thread is QThread(0x1eaf27f09d0), current thread is QThreadPoolThread(0x1eaf4939fd0)
      

      My code looks like this

          //Constructor
          connect(this, &EffectTable::NeedWidget,  this, &EffectTable::PrepWidget, Qt::QueuedConnection);
          QtConcurrent::run(&EffectTable::ReadINI, this);
          //Constructor
      
      void EffectTable::ReadINI()
      {
          QSettings set("...", QSettings::IniFormat, this);
      
          set.beginGroup("Basic");
          QStringList list = set.value("ID").toStringList();
          qint32      size = list.size();
          set.endGroup();
      
          emit NeedWidget(count);
      }
      
      void EffectTable::PrepWidget(qint32 count)
      {
          QComboBox*   cb0 = new QComboBox[count];        //Error here
          QPushButton* pb0 = new QPushButton[count];
      
      }
      
      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        You can not create widgets in a different thread than the main gui thread (and I don't see a reason for this here).

        Qt has to stay free or it will die.

        M 1 Reply Last reply Reply Quote 1
        • M
          MasterBlade @Christian Ehrlicher last edited by

          @Christian-Ehrlicher said in How to create a QComboBox in another thread?:

          You can not create widgets in a different thread than the main gui thread (and I don't see a reason for this here).

          So my problem is, How to switch to the main thread?

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @MasterBlade last edited by

            @MasterBlade said in How to create a QComboBox in another thread?:

            So my problem is, How to switch to the main thread?

            using signals/slots

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply Reply Quote 3
            • M
              MasterBlade @jsulm last edited by

              @jsulm said in How to create a QComboBox in another thread?:

              @MasterBlade said in How to create a QComboBox in another thread?:

              So my problem is, How to switch to the main thread?

              using signals/slots

              You see my code, I was trying to emit signals. Maybe it was not working as expected...

              jsulm 1 Reply Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion @MasterBlade last edited by

                @MasterBlade I'm not sure what the problem is, but I guess the reason is that you use same object in main thread and in an additional thread. I suggest you do not emit a signal, but use QFutureWatcher which has https://doc.qt.io/qt-5/qfuturewatcher.html#finished signal. So, your ReadINI() will return count which you then can get from the QFuture as soon as https://doc.qt.io/qt-5/qfuturewatcher.html#finished is emitted.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 2
                • First post
                  Last post