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. How to create a QComboBox in another thread?
QtWS25 Last Chance

How to create a QComboBox in another thread?

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

    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
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      M 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

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

        M Offline
        M Offline
        MasterBlade
        wrote on last edited by
        #3

        @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?

        jsulmJ 1 Reply Last reply
        0
        • M MasterBlade

          @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?

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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
          3
          • jsulmJ jsulm

            @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

            M Offline
            M Offline
            MasterBlade
            wrote on last edited by
            #5

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

            jsulmJ 1 Reply Last reply
            0
            • M MasterBlade

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

              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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
              2

              • Login

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