简体   繁体   中英

Widgets must be created in the main thread error in Qt

I have a problem in Qt.

I have a external pthread that need to show a Qt Dialog. I have created a class WindowsManager that contain a static function to show a Qt Dialog: WindowsManager::ShowDialog()

When call this function in my pthread

void* task(void*)
{
    WindowsManager::ShowDialog();
}

Then Qt shows me an Assertion Failed:

Widgets must be created in the main thread

Can I get around this issue?

Make the WindowsManager ShowDialog a slot, which it will be invoked from the current thread.

Assuming the WindowsManager has been created, move the WindowsManager class to the main thread: -

windowsManager->moveToThread(QApplication::instance()->thread());

Then invoke the slot with QMetaObject::invoke

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM