Creating Modal and Modeless Dialogs
Modal Dialogs
The user cannot interact with the main window until the dialog is closed.
Typically used for critical information or decisions that require the user’s focus.
Modeless Dialogs
Even if the dialog is open, the user can interact with the main window simultaneously.
1# -*- coding: utf-8 -*-
2
3import sys
4from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QDialog, QVBoxLayout, QLabel, QWidget
5
6class MainWindow(QMainWindow):
7 def __init__(self):
8 super(MainWindow, self).__init__()
9
10 self.init_ui()
11
12 def init_ui(self):
13 self.button_modal = QPushButton("Open Modal Dialog", self)
14 self.button_modal.clicked.connect(self.show_modal)
15 self.button_modal.move(10, 10)
16 self.button_modal.resize(150, 40)
17
18 self.button_modeless = QPushButton("Open Modeless Dialog", self)
19 self.button_modeless.clicked.connect(self.show_modeless)
20 self.button_modeless.move(10, 60)
21 self.button_modeless.resize(150, 40)
22
23 def show_modal(self):
24 self.modal_dialog = QDialog(self)
25 self.modal_dialog.setWindowTitle("Modal Dialog")
26
27 layout = QVBoxLayout()
28 label = QLabel("This is a Modal Dialog. You can't interact with the main window until you close this.")
29 layout.addWidget(label)
30
31 self.modal_dialog.setLayout(layout)
32 self.modal_dialog.exec_()
33
34 def show_modeless(self):
35 self.modeless_dialog = QDialog(self)
36 self.modeless_dialog.setWindowTitle("Modeless Dialog")
37
38 layout = QVBoxLayout()
39 label = QLabel("This is a Modeless Dialog. You can still interact with the main window.")
40 layout.addWidget(label)
41
42 self.modeless_dialog.setLayout(layout)
43 self.modeless_dialog.show()
44
45if __name__ == '__main__':
46 app = QApplication(sys.argv)
47 mainWin = MainWindow()
48 mainWin.show()
49 sys.exit(app.exec_())
Difference between exec_() and show()
exec_() and show() are two main methods in PyQt used for displaying dialogs or widgets. Their primary differences are as follows:
Block vs Non-Block
exec_(): - This method isblocking. - This means when theexec_()method is called, the execution of the code pauses until that dialog is closed. - It’s mainly used in modal dialogs. - It restricts the user from interacting with other parts of the application until they respond to the dialog.show(): - This method isnon-blocking. - When theshow()method is called, the dialog is displayed and the code continues to run. - It’s mainly used in modeless dialogs. - It allows the user to interact with other parts of the application alongside the dialog.
Return Value
exec_(): - Has a return value. - Typically, it returns one of the valuesQDialog.AcceptedorQDialog.Rejected. - This can indicate whether the user clicked the ‘OK’ or ‘Cancel’ button in the dialog.show(): - Does not have a return value.
Use Cases
exec_(): - Mainly used when waiting for a user’s response is necessary, like when needing to wait for user choices in a popup that asks for information input.show(): - Mainly used when just providing information or when not necessarily awaiting a user’s decision.
In conclusion, the method to use depends on the purpose of the dialog and the desired interaction with the user.