小部件必须在Qt的主线程错误中创建
Widgets must be created in the main thread error in Qt
我在Qt中有一个问题。
我有一个需要显示Qt对话框的外部pthread。我创建了一个类WindowsManager
,其中包含一个静态函数来显示Qt对话框:WindowsManager::ShowDialog()
在我的pthread中调用这个函数
void* task(void*)
{
WindowsManager::ShowDialog();
}
然后Qt显示我一个断言失败:
部件必须在主线程中创建
我能解决这个问题吗?
使WindowsManager ShowDialog成为一个插槽,它将从当前线程调用。
假设已经创建了WindowsManager,将WindowsManager类移动到主线程:-
windowsManager->moveToThread(QApplication::instance()->thread());
然后用QMetaObject::invoke