How to create a QComboBox in another thread?
-
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]; }
-
You can not create widgets in a different thread than the main gui thread (and I don't see a reason for this here).
-
You can not create widgets in a different thread than the main gui thread (and I don't see a reason for this here).
@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?
-
@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?
@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
-
@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
@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 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...
@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.