Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Show a messagebox until a thread emits a Signal
QtWS25 Last Chance

Show a messagebox until a thread emits a Signal

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 4.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dualfaces
    wrote on last edited by
    #1

    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");
    m_box->show();
    }

    public void on_ConnectionEstablished() {
    if (m_box != nullptr) {
    m_box->close();
    delete m_box;
    m_box = nullptr;
    }
    //restart processing
    }
    @

    Thank you!

    Regards

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #3

        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.

        Greetz, Jeroen

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dualfaces
          wrote on last edited by
          #4

          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.

          regards

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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 ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dualfaces
              wrote on last edited by
              #6

              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->setText("Wait");
              m_box->setStandardButtons(QMessageBox::Cancel);
              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?

              Regards

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved