Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QMessageBox hangs but doesn't show with exec()

QMessageBox hangs but doesn't show with exec()

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 2 Posters 932 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
    DoubleFelix
    wrote on last edited by
    #1

    I have a dialog popup defined like so:

    class ClientWindow(QMainWindow):
        def __init__(self):
            super(ClientWindow, self).__init__()
            loadUi(os.path.join(os.path.dirname(__file__), "window.ui"), self)
    
        def display_text_box(self, text):
            text_box = QMessageBox()
            text_box.setWindowTitle("Alert!")
            text_box.setText(text)
            text_box.setIcon(QMessageBox.Warning)
            text_box.setStandardButtons(QMessageBox.Ok)
            print("before show")
            text_box.exec()
            print("after show")
            self.close()
    

    display_text_box() is called by another window, and when I attempt to show the window, it just makes the ClientWindow stop responding.
    I know exec() is supposed to hang, since it doesn't return until the window is closed unlike show(), but the window doesn't show up either. Weirdly, in another window that's defined in the same way but inherits QDialog instead, a popup dialog window appears using the exact same code.

    Is the window not showing related to the "parent" class inheriting QMainWindow? If so, what's the difference between QDialog and QMainWindow and will it have any drastic side effects if I switch?

    JonBJ 1 Reply Last reply
    0
    • D DoubleFelix

      I have a dialog popup defined like so:

      class ClientWindow(QMainWindow):
          def __init__(self):
              super(ClientWindow, self).__init__()
              loadUi(os.path.join(os.path.dirname(__file__), "window.ui"), self)
      
          def display_text_box(self, text):
              text_box = QMessageBox()
              text_box.setWindowTitle("Alert!")
              text_box.setText(text)
              text_box.setIcon(QMessageBox.Warning)
              text_box.setStandardButtons(QMessageBox.Ok)
              print("before show")
              text_box.exec()
              print("after show")
              self.close()
      

      display_text_box() is called by another window, and when I attempt to show the window, it just makes the ClientWindow stop responding.
      I know exec() is supposed to hang, since it doesn't return until the window is closed unlike show(), but the window doesn't show up either. Weirdly, in another window that's defined in the same way but inherits QDialog instead, a popup dialog window appears using the exact same code.

      Is the window not showing related to the "parent" class inheriting QMainWindow? If so, what's the difference between QDialog and QMainWindow and will it have any drastic side effects if I switch?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @DoubleFelix
      This should indeed work as-is. I don't know what "called by another window" means, given that display_text_box() is defined in ClientWindow/the main window. I would start from a standalone example to check it works, and then build towards whatever situation you are in here.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DoubleFelix
        wrote on last edited by
        #3

        I don't know what "called by another window" means

        I have this window class defined in another, and display_text_box is called by that class.

        JonBJ 1 Reply Last reply
        0
        • D DoubleFelix

          I don't know what "called by another window" means

          I have this window class defined in another, and display_text_box is called by that class.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @DoubleFelix
          I don't know what you're doing defining a method in a class derived from QMainWindow, where the method displays a modal dialog and you're calling it from outside the main window. Whether that has any bearing on your issue I don't know.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DoubleFelix
            wrote on last edited by
            #5

            How should I do it then?

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

              Is it possible that the 2 already open windows are causing some error?

              JonBJ 1 Reply Last reply
              0
              • D DoubleFelix

                Is it possible that the 2 already open windows are causing some error?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @DoubleFelix
                I don't know what your code is. But (IMHO anyway) no other class should call any method in the main window class, nor have any access to it, nor import it. And I cannot see why the main window would want to export a method whose job is to show a message box. Further, that method ends by closing the main window, and it did not even open it.

                Start by redesigning some clean & logical flow. Why is some ClientWindow derived from QMainWindow? If you need this message box to be show, why can't the other place create and show it? Or, if for some reason it's necessary to have the main window show it, have the outside world emit a signal requesting it and have the main window slot onto that to show the dialog.

                1 Reply Last reply
                2

                • Login

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