Display a QMessageBox from a QThread

时间:2021-12-12 10:50:43

Emit a signal. Since you cannot do UI stuff in a Qthread, instead send your message as an argument of your signal.

signal decalaration in your qthread:

signals:
  void write2SysStatus(QString theMessage);

 

emitting the signal from the qthread:

emit write2SysStatus("Some status");

 

slot declaration/definition in QMainWindow:

public slots:
  void eWriteLine ( QString theMessage ){
       //this is where you use you message box.
  }

 

connecting of the slot and signal:

connect(pFPSengine, SIGNAL(write2SysStatus(QString)), this,SLOT(eWriteLine(QString)));

 

https://*.com/questions/4299493/display-a-qmessagebox-from-a-qthread

 

You can't create widgets in any other thread than the main thread (the one from app.exec()) If you want to popup a message box based on something happening in a thread, you must emit a signal from that thread back to e.g. your main window and use your message box there

 

https://forum.qt.io/topic/32255/pop-up-a-qmessagebox-from-a-qthread