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. QMessageBox dose not show the content
QtWS25 Last Chance

QMessageBox dose not show the content

Scheduled Pinned Locked Moved Solved General and Desktop
dialogmodal dialogevent handlingmessagebox
12 Posts 4 Posters 3.0k 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
    dalishi
    wrote on last edited by
    #1

    Hi,

    I want to pop up a dialog telling the user to wait for processing. I use show() instead of exec() as I want the message box to return to the caller immediately (below is the code i used.). However, the message box only shows its frame but not any contents. (showing the residue of previous windows as in the attached image). Any idea how to solve this? The exec() works fine by the way but it only returns when the dialog is closed, which is not the use case here.

    0_1560238806485_Screenshot from 2019-06-11 11-08-45.png

    QMessageBox msgBox(QMessageBox::Information, tr("Request Destination"),
                                       "Waiting for reply ...", QMessageBox::Cancel, this);
    msgBox.setModal(true);
    msgBox.show();
    QApplication::processEvents();
    
    while (...) {
        dowork();
    }
    
    jsulmJ JonBJ 2 Replies Last reply
    0
    • D dalishi

      Hi,

      I want to pop up a dialog telling the user to wait for processing. I use show() instead of exec() as I want the message box to return to the caller immediately (below is the code i used.). However, the message box only shows its frame but not any contents. (showing the residue of previous windows as in the attached image). Any idea how to solve this? The exec() works fine by the way but it only returns when the dialog is closed, which is not the use case here.

      0_1560238806485_Screenshot from 2019-06-11 11-08-45.png

      QMessageBox msgBox(QMessageBox::Information, tr("Request Destination"),
                                         "Waiting for reply ...", QMessageBox::Cancel, this);
      msgBox.setModal(true);
      msgBox.show();
      QApplication::processEvents();
      
      while (...) {
          dowork();
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @dalishi You're blocking the event loop with your while loop, no wonder dialog can't render correctly...
      If you want to show dialog and and do something at the same time you can move the dowork() part to a thread.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      2
      • D dalishi

        Hi,

        I want to pop up a dialog telling the user to wait for processing. I use show() instead of exec() as I want the message box to return to the caller immediately (below is the code i used.). However, the message box only shows its frame but not any contents. (showing the residue of previous windows as in the attached image). Any idea how to solve this? The exec() works fine by the way but it only returns when the dialog is closed, which is not the use case here.

        0_1560238806485_Screenshot from 2019-06-11 11-08-45.png

        QMessageBox msgBox(QMessageBox::Information, tr("Request Destination"),
                                           "Waiting for reply ...", QMessageBox::Cancel, this);
        msgBox.setModal(true);
        msgBox.show();
        QApplication::processEvents();
        
        while (...) {
            dowork();
        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @dalishi
        Suggest you might consider changing over to use https://doc.qt.io/qt-5/qprogressdialog.html for this, which will spin the necessary updating event loop.

        D 1 Reply Last reply
        1
        • jsulmJ jsulm

          @dalishi You're blocking the event loop with your while loop, no wonder dialog can't render correctly...
          If you want to show dialog and and do something at the same time you can move the dowork() part to a thread.

          D Offline
          D Offline
          dalishi
          wrote on last edited by
          #4

          @jsulm Thanks for the prompt reply. I thought the processEvents() does the tricks. If not, I guess what you suggested is the only way.

          jsulmJ 1 Reply Last reply
          0
          • D dalishi

            @jsulm Thanks for the prompt reply. I thought the processEvents() does the tricks. If not, I guess what you suggested is the only way.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @dalishi said in QMessageBox dose not show the content:

            I thought the processEvents() does the tricks

            How can it if it's not inside the loop?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • JonBJ JonB

              @dalishi
              Suggest you might consider changing over to use https://doc.qt.io/qt-5/qprogressdialog.html for this, which will spin the necessary updating event loop.

              D Offline
              D Offline
              dalishi
              wrote on last edited by dalishi
              #6

              @JonB Thank you for bringing up progressdialog. Actually i have progressdialog in my program and the similar thing happens. For the first few steps, the progressdialog also does not show anything but frame only. Fortunately it shows the progress bar after a few more steps. I think it's also because the following processing code blocks the event loop. I'm just curious how the event loop works and how should we ask Qt to process the event at certain point.

              jsulmJ 1 Reply Last reply
              0
              • D dalishi

                @JonB Thank you for bringing up progressdialog. Actually i have progressdialog in my program and the similar thing happens. For the first few steps, the progressdialog also does not show anything but frame only. Fortunately it shows the progress bar after a few more steps. I think it's also because the following processing code blocks the event loop. I'm just curious how the event loop works and how should we ask Qt to process the event at certain point.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @dalishi Either move processEvents() INSIDE the loop, or (which is better as processEvents() is a bad work around) move your background work to another thread.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                D 1 Reply Last reply
                1
                • J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  This is untested, but should work as well. Without the nasty QProcessEvents.

                  QMessageBox msgBox(QMessageBox::Information, tr("Request Destination"),"Waiting for reply ...", QMessageBox::Cancel, this);
                  msgBox.setModal(true);
                  
                  QMetaObject::invokeMethod(this, [=]()->void{
                      while (...) {
                          dowork();
                      }
                  }, Qt::QueuedConnection);
                  
                  msgBox.exec();
                  
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  jsulmJ D 2 Replies Last reply
                  0
                  • jsulmJ jsulm

                    @dalishi Either move processEvents() INSIDE the loop, or (which is better as processEvents() is a bad work around) move your background work to another thread.

                    D Offline
                    D Offline
                    dalishi
                    wrote on last edited by
                    #9

                    @jsulm Thanks very much. I finally got what you mean. processEvent() need to be in the loop for periodically getting called and processing the event. I just tried put it in the loop but I think this way messed up the normal event handling of Qt. The buttons and windows are not responsive any more as usual. So I will go for the second option as per your instruction. Thanks.

                    1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      This is untested, but should work as well. Without the nasty QProcessEvents.

                      QMessageBox msgBox(QMessageBox::Information, tr("Request Destination"),"Waiting for reply ...", QMessageBox::Cancel, this);
                      msgBox.setModal(true);
                      
                      QMetaObject::invokeMethod(this, [=]()->void{
                          while (...) {
                              dowork();
                          }
                      }, Qt::QueuedConnection);
                      
                      msgBox.exec();
                      
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @J.Hilk Your solution will block the event loop as well as soon as the lambda is called, right?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      J.HilkJ 1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk

                        This is untested, but should work as well. Without the nasty QProcessEvents.

                        QMessageBox msgBox(QMessageBox::Information, tr("Request Destination"),"Waiting for reply ...", QMessageBox::Cancel, this);
                        msgBox.setModal(true);
                        
                        QMetaObject::invokeMethod(this, [=]()->void{
                            while (...) {
                                dowork();
                            }
                        }, Qt::QueuedConnection);
                        
                        msgBox.exec();
                        
                        
                        D Offline
                        D Offline
                        dalishi
                        wrote on last edited by
                        #11

                        @J.Hilk Hi thanks. I just tested the solution but it does not work. The message box does not show content until the while loop job is finished.

                        1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @J.Hilk Your solution will block the event loop as well as soon as the lambda is called, right?

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #12

                          @jsulm
                          actually, it doesn't work at all, from my quick test I made.

                          I'm not entirely sure why...


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          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