Can I use show()/exec() in a Secondary thread
-
I use the QDialog::show() in the GUI-thread ans a secondary thread, and it was crashed.
So shouldn't I use that in a secondary thread?void func() { QDialog *dialog = new QDialog(nullptr); dialog->setAttribute(Qt::WA_DeleteOnClose); dialog->setWindowTitle(QObject::tr("Hello, dialog!")); dialog->show(); } int main(int argc, char** argv) { QApplication a(argc, argv); QDialog *w = new QDialog(nullptr); w->setWindowTitle(QObject::tr("Hello, dialog!")); auto ThreadInitResult = std::async(std::launch::async, [=]() { func(); }); w->show(); return a.exec(); }
-
@Toocold said in Can I use show()/exec() in a Secondary thread:
So shouldn't I use that in a secondary thread?
Correct. You cannot call GUI class functions in a secondary thread.
Can I use show()/exec() in a Secondary thread
No.
-
@Toocold said in Can I use show()/exec() in a Secondary thread:
So I might use the signal/slot instead of?
What do you want to do in the secondary thread?
-
@JKSH I need call GUI class functions in a secondary thread, like the Dialog::show().
Is it safe to connect a signal(like a function named notify()) to a slot(like a function named Onnotify(), ans Onnotify() will call the Dialog::show()) and emit the notify() in the secondary thread. -
@Toocold said in Can I use show()/exec() in a Secondary thread:
I need call GUI class functions in a secondary thread, like the Dialog::show()
If you only want to ensure calling Dialog::show() in the right thread, you can:
- use signal/slot connection
- use
QTimer::singleShot()
when your class instance in available in the calling object, like this:QTimer::singleShot(0, &m_myDialog, &Dialog::show);
-
QTimer::singleShot(0, &m_myDialog, &Dialog::show);
Wow, that's a bit cheaty ;-) So you get a thread to cause the UI thread (that's where the static
QTimer::singleShot
runs?) to do the open for you (immediately), without having to create a specific signal/slot from the thread to the UI to implement? -
Hi,
@Toocold said in Can I use show()/exec() in a Secondary thread:
@JKSH I need call GUI class functions in a secondary thread, like the Dialog::show().
That doesn't answer @JKSH question: what do you want to do with that dialog ?
-
@JonB said in Can I use show()/exec() in a Secondary thread:
Wow, that's a bit cheaty ;-) So you get a thread to cause the UI thread (that's where the static QTimer::singleShot runs?) to do the open for you (immediately), without having to create a specific signal/slot from the thread to the UI to implement?
QTimer::singleShot() will:
- create a temporary QTimer object, which runs in the current thread
- connect to QTimer::timeout signal to the passed slot (and to deleteLater() of the temporary timer)
- start the temporary timer
This done under the hood.
-
@KroMignon said in Can I use show()/exec() in a Secondary thread:
- use
QTimer::singleShot()
when your class instance in available in the calling object, like this:QTimer::singleShot(0, &m_myDialog, &Dialog::show);
Or
QMetaObject::invokeMethod(myDialog, "show");
- use
-
@Toocold said in Can I use show()/exec() in a Secondary thread:
Is it safe to connect a signal(like a function named notify()) to a slot(like a function named Onnotify(), ans Onnotify() will call the Dialog::show()) and emit the notify() in the secondary thread.
Yes, it is safe to connect signals and slots betwen different threads.
However, you must make sure your QDialog is constructed in your QApplication thread.