PyQt5 QDialog
We will start with the QDialog widget. The QDialog class is the base class of dialog windows. Dialog windows are used to provide messages or ask questions. A dialog window can also be used to warn users or ask for confirmation. Let’s create a simple dialog.
There are two functions show() and exec() for QDialog display. The difference between them is explained in the reference document as follows.
-
show(): Displays a non-modal dialog. Control is immediately returned to the calling function. Whether the popup window is a modal dialog or not depends on the value of the modal property.
-
exec(): Displays a modal dialog and locks the program until the user closes the dialog. The function returns a DialogCode result. During the dialog popup, the user cannot switch to other windows under the same program until the dialog is closed.
Modal and non-modal
A modal dialog is one in which the whole program is locked when the window is popped up and is in a waiting state until the dialog is closed. This is often the case when the return value of the dialog box is needed for the following operation. For example, confirm the window (select “Yes” or “No”). For non-modal dialogs, after calling the popup window, the call returns immediately and continues the following operation. It is just a call command issued here, without waiting and without any processing. E.g. Find box.
QDialog example
First of all, these two methods return different values. exec() has a return value, show() has no return value.
Secondly, these two methods also have different roles. Calling show() only shows the widget and its contents, and control is returned to the calling function immediately. After calling exec(), the calling thread will be blocked and the program will be locked until the user closes the dialog, during which the user cannot switch to other windows under the same program until the Dialog is closed.
In the example of a parent class clicking on the event layer QDialog child form
The parent class instantiates the child class directly with the .show() method, so it has to be instantiated as a global variable of the parent class or execute the exec_() method
#!/usr/bin/python
# -*- coding: utf-8 -*-
from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QWidget, QDialog, QApplication, QPushButton, QVBoxLayout
import sys
class W1(QWidget):
def __init__(self, parent=None):
super(W1, self). __init__(parent)
self.btn = QPushButton('Click1')
vb = QVBoxLayout()
vb.addWidget(self.btn)
self.setLayout(vb)
self.btn.clicked.connect(self.fireupWindows2)
def fireupWindows2(self):
w2 = W2() # w2= W2(self) in parent class base self popup box
w2.show() # Dialog show() direct flashback
# Need to instantiate as a global variable by self, without self, it will be recycled as soon as it runs and will not be displayed.
self.w2 = W2()
self.w2.show()
# or directly exec_() Dialog can execute exec_() method, Widget does not have exec_() method
w2 = W2()
w2.exec_() # app.exec_()
class W2(QDialog):
def __init__(self, parent=None):
super(W2, self). __init__(parent)
self.btn = QPushButton('Click2')
vb = QVBoxLayout()
vb.addWidget(self.btn)
self.setLayout(vb)
self.btn.clicked.connect(self.fireupWindows3)
def fireupWindows3(self):
self.accept()
if __name__ == "__main__":
app = QApplication(sys.argv)
w = W1()
w.show()
sys.exit(app.exec_())