Show a messagebox until a thread emits a Signal

  • Hello,

    if I loose a connection to device I want to open a message box which says that the application waits for the connection to be reestablished.
    Therefore, when i recognize the lost connection i start a thread which will run until the connection is established again. Then it emits a Signal which says that the connection is established.
    This signal is connected to a slot of my gui thread, but unfortunately it does not close the messagebox, cause the thread is stuck in showing the messagebox.
    Is there a proper way to do this? Here is a snippet of my code
    QMessageBox* m_box; //private class variable of gui thread

    public void on_ConnectionLost() {
    m_box = new QMessageBox();
    m_box->setText("Wait for connection to return");

    public void on_ConnectionEstablished() {
    if (m_box != nullptr) {
    delete m_box;
    m_box = nullptr;
    //restart processing

    Thank you!


  • Lifetime Qt Champion


    AFAIK, calling show() should not block anything, are you sure on_ConnectionEstablished is called ?

  • What happens to your thread after it has re-established the connection? Do you terminate it? If so, use deleteLater() instead of delete(). This way your event queue will be finished before deletion.

  • So the m_box->show() call is indeed not blocking. And on_connectionEstablished is executed. When stepping through with the debugger the message box is successfully closed, when i run it without breakpoints the box does not disappear.
    Since i run it on an android device the problem might also stem from the cross-platform debugger.
    The thread itself (the run() function) terminates after establishing the connection, but the object stays alive.


  • Lifetime Qt Champion

    When running without the debugger, are you sure the message box is still there and not only painted ? Can you click on a button of it ?

  • Hi,

    it was just the problem that the Qmessagebox was not removed from the screen, the application was working normally behind it.
    Now i uses m_box->done(0); and the box disappears.
    Now i struggle with adding a button to cancel waiting and close the box and getting the fired signal.
    Therefore I modified my m_box to
    m_box = new QMessageBox();
    m_box->setButtonText(QMessageBox::Cancel, "Do not Wait");
    connect(m_box, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(on_cancelWaitingClicked(QAbstractButton*)));

    The button is added (although it is no renamed, it's text is still Cancel. But when i click the button the slot is not executed.
    The messagebox is still shown via m_box->show().
    Isn't it possible to connect a slot this way?


  • Lifetime Qt Champion

    Then why not use a QProgressDialog ? I would be more suited to your needs

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.