Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

show() and exec_() behavior difference



  • Hello.
    I have spent many time for solving the problem.

    I made code that display QDialog window when a condition meet.

    Here is part of code

        def display_message(self):
            global result_display
    
            self.Dialog = QtWidgets.QDialog()
            self.dm = Warning_Window(result_display)
            self.dm.setupUi(self.Dialog)
            self.Dialog.exec_()
    

    When I use self.Dialog.exec_(), QDialog window displays
    but in case of self.Dialog.show(), my program does not response

    Below is code
    main.py

    class Form(QMainWindow, Ui_MainWindow):
        def __init__(self, parent=None):
            super().__init__()
            self.setupUi(self)
            self.show()
    
        @pyqtSlot()
        def display_message(self):
            global result_display
    
            self.Dialog = QtWidgets.QDialog()
            self.dm = Warning_Window(result_display)
            self.dm.setupUi(self.Dialog)
            self.Dialog.exec_()
    
    
        def discover_sleeping_cell(self):
    
            global order
            global result_display
    
    
                if (len(result_display) != 0):
    
                    p1 = Thread(target=self.display_message)
                    p1.start()
    
    
            cursor.close()
            conn.close()
            fp_sleeping_cell.close()
    
            order = order + 1
            if order > 32:
                pass
            else :
                self.wait_10min()
    
    
        def wait_10min(self):
            global order
    
            t = threading.Timer(1.0,self.discover_sleeping_cell)
            t.start()
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        global w
        w = Form()
    
        file_list = []
        order = 0
        w.wait_10min()
    
        sys.exit(app.exec_())
    

    warning_window.py

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'C:\Users\think\Documents\monitor\warning_window.ui'
    #
    # Created by: PyQt5 UI code generator 5.13.0
    #
    # WARNING! All changes made in this file will be lost!
    
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Warning_Window(object):
    
        def __init__(self,msg):
            global list_of_sleeping_cell
    
            list_of_sleeping_cell = msg
            if list_of_sleeping_cell == 'none':
                pass
            print(list_of_sleeping_cell)
    
        def setupUi(self, dialog):
    
            dialog.setObjectName("Dialog")
            dialog.resize(500, 400)
            self.horizontalLayout = QtWidgets.QHBoxLayout(dialog)
            self.horizontalLayout.setObjectName("horizontalLayout")
    
            self.warn_sleeping = QtWidgets.QTextBrowser(dialog)
    
            self.warn_sleeping.setFrameShape(QtWidgets.QFrame.StyledPanel)
            self.warn_sleeping.setFrameShadow(QtWidgets.QFrame.Plain)
            self.warn_sleeping.setLineWidth(3)
            self.warn_sleeping.setMidLineWidth(1)
    
            self.warn_sleeping.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
            self.warn_sleeping.setObjectName("warn_sleeping")
            self.horizontalLayout.addWidget(self.warn_sleeping)
    
            self.retranslateUi(dialog)
            QtCore.QMetaObject.connectSlotsByName(dialog)
    
            ################################################
            lenth_sleeping_cell_list = len(list_of_sleeping_cell)
    
            self.warn_sleeping.setText("Message")
    
    
            ################################################
    
        def retranslateUi(self, dialog):
            _translate = QtCore.QCoreApplication.translate
            dialog.setWindowTitle(_translate("Dialog", "Dialog"))
            self.warn_sleeping.setText(_translate("Dialog", "TextLabel"))
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        Dialog = QtWidgets.QDialog()
        ui = Warning_Window()
        ui.setupUi(Dialog)
        Dialog.show()
        sys.exit(app.exec_())
    
    


  • @Junhi said in show() and exec_() behavior difference:

    but in case of self.Dialog.show(), my program does not response

    Don't know what that means.

    show() and exec() work as documented, whether in C++ or Python.

    p1 = Thread(target=self.display_message): what are you doing trying to use threads here, especially with Python? Further, you are trying to make the thread execute a Qt UI behaviour, which is absolutely forbidden in Qt (UI stuff can only be executed in the main GUI thread), and could easily be your issue.

    Why does your warning_window.py have all the if __name__ == "__main__": code, it's just confusing?

    So start by sorting out your code, in particular remove any thread stuff, and see where you are then.



  • @JonB Thank you for your feedback

    1. "but in case of self.Dialog.show(), my program does not response" means
      When I use self.Dialog.exec_() at 5th line in the display_message() method,
      I can see QDialog window pop-up.
      But when I use self.Dialog.show() instead of self.Dialog.exec_(), QDialog window does not pop-up and main program got hang(no response).

    2. p1 = Thread(target=self.display_message): could easily be your issue.
      I have changed the code like below, but same result

               if (len(result_display) != 0):
    
    #                p1 = Thread(target=self.display_message)
    #                p1.start()
                    self.display_message()
    
    1. ave all the if name == "main": code, it's just confusing?
      Yes, it is just confusing. This is my first GUI code.
      So difficult than non GUI programming. -_-;;
      What is your recommendation?

  • Qt Champions 2019

    @Junhi Why do you have

    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        Dialog = QtWidgets.QDialog()
        ui = Warning_Window()
        ui.setupUi(Dialog)
        Dialog.show()
        sys.exit(app.exec_())
    

    in warning_window.py ?



  • @jsulm Hmmm.. I don't know why.
    Cause warning_window.py is from QT Designer.
    I just design by using QT Design and convert .ui to .py



  • @Junhi said in show() and exec_() behavior difference:

    @jsulm Hmmm.. I don't know why.
    Cause warning_window.py is from QT Designer.
    I just design by using QT Design and convert .ui to .py

    I had just removed main and had test again. but Same result

    #if __name__ == "__main__":
    #    import sys
    
    #    app = QtWidgets.QApplication(sys.argv)
    #    Dialog = QtWidgets.QDialog()
    #    ui = Warning_Window()
    #    ui.setupUi(Dialog)
    #    Dialog.show()
    #    sys.exit(app.exec_())
    
    

Log in to reply