|
24 | 24 | # Source: https://github.com/Erriez/pyside6-getting-started |
25 | 25 | # |
26 | 26 |
|
27 | | -from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QVBoxLayout,\ |
28 | | - QDialog, QFormLayout, QLineEdit, QDialogButtonBox, QMessageBox, QSpinBox |
29 | | -from PySide6.QtCore import Qt |
| 27 | +from PySide6.QtWidgets import ( |
| 28 | + QApplication, |
| 29 | + QWidget, |
| 30 | + QDialog, |
| 31 | + QDialogButtonBox, |
| 32 | + QLabel, |
| 33 | + QLineEdit, |
| 34 | + QPushButton, |
| 35 | + QHBoxLayout, |
| 36 | + QVBoxLayout, |
| 37 | + QMessageBox |
| 38 | +) |
30 | 39 | import sys |
31 | 40 |
|
32 | 41 |
|
33 | | -# https://doc.qt.io/qtforpython/PySide6/QtWidgets/QDialog.html |
34 | | -class CustomDialog(QDialog): |
35 | | - def __init__(self, parent=None, title='', name='', age=0): |
| 42 | +class Dialog(QDialog): |
| 43 | + def __init__(self, parent=None): |
36 | 44 | super().__init__(parent) |
37 | 45 |
|
38 | | - # Resize window |
39 | | - self.setMinimumWidth(300) |
40 | | - self.setWindowTitle(title) |
| 46 | + # Set window size and title |
| 47 | + self.setMinimumSize(250, 150) |
| 48 | + self.setWindowTitle("Resizable dialog") |
41 | 49 |
|
42 | | - # Create name text box |
43 | | - self.txt_name = QLineEdit(name) |
44 | | - self.txt_name.setMaxLength(20) |
| 50 | + # Create label and line edit, then add to horizontal box layout |
| 51 | + self.labelName = QLabel("Your name:") |
| 52 | + self.editName = QLineEdit() |
| 53 | + self.horizontalLayout = QHBoxLayout() |
| 54 | + self.horizontalLayout.addWidget(self.labelName) |
| 55 | + self.horizontalLayout.addWidget(self.editName) |
45 | 56 |
|
46 | | - # Create age spinbox |
47 | | - self.spin_age = QSpinBox() |
48 | | - self.spin_age.setMinimum(0) |
49 | | - self.spin_age.setMaximum(100) |
50 | | - self.spin_age.setValue(age) |
| 57 | + # Create a dialog button box for OK and Cancel buttons |
| 58 | + self.buttonBox = QDialogButtonBox() |
| 59 | + self.buttonBox.setStandardButtons(QDialogButtonBox.StandardButton.Cancel | QDialogButtonBox.StandardButton.Ok) |
51 | 60 |
|
52 | | - # Add text boxes to form layout |
53 | | - self.form = QFormLayout() |
54 | | - self.form.addRow('Name: ', self.txt_name) |
55 | | - self.form.addRow('Age:', self.spin_age) |
| 61 | + # Connect OK and Cancel buttons to accept / reject slot |
| 62 | + self.buttonBox.accepted.connect(self.accept) |
| 63 | + self.buttonBox.rejected.connect(self.reject) |
56 | 64 |
|
57 | | - # Create dialog Ok and Cancel buttons |
58 | | - dialog_buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | |
59 | | - QDialogButtonBox.StandardButton.Cancel) |
60 | | - dialog_buttons.accepted.connect(self.accept) |
61 | | - dialog_buttons.rejected.connect(self.reject) |
| 65 | + # Add previously created layout and widgets to the dialog |
| 66 | + self.vboxLayout = QVBoxLayout(self) |
| 67 | + self.vboxLayout.addLayout(self.horizontalLayout) |
| 68 | + self.vboxLayout.addStretch() # <- Stretch between layout on the top and OK/Cancel buttons at the bottom |
| 69 | + self.vboxLayout.addWidget(self.buttonBox) |
62 | 70 |
|
63 | | - # Add form and dialog buttons to vertical box layout |
64 | | - vbox = QVBoxLayout() |
65 | | - vbox.addLayout(self.form) |
66 | | - vbox.addWidget(dialog_buttons) |
67 | 71 |
|
68 | | - # Add vertical layout to window |
69 | | - self.setLayout(vbox) |
| 72 | +class Window(QWidget): |
| 73 | + def __init__(self): |
| 74 | + super().__init__() |
70 | 75 |
|
71 | | - def get_name(self): |
72 | | - # Return text from textbox |
73 | | - return self.txt_name.text() |
| 76 | + # Set window size and title |
| 77 | + self.setMinimumSize(250, 150) |
| 78 | + self.setWindowTitle("Main window") |
74 | 79 |
|
75 | | - def get_age(self): |
76 | | - # Return age from spinbox |
77 | | - return self.spin_age.value() |
| 80 | + # Add one button to open dialog |
| 81 | + self.buttonShowDialog = QPushButton("Show dialog") |
| 82 | + self.buttonShowDialog.clicked.connect(self.on_show_dialog_model) |
78 | 83 |
|
| 84 | + # Add button to vertical box layout |
| 85 | + self.vboxLayout = QVBoxLayout(self) |
| 86 | + self.vboxLayout.addWidget(self.buttonShowDialog) |
79 | 87 |
|
80 | | -class MainWindow(QWidget): |
81 | | - def __init__(self): |
82 | | - super().__init__() |
| 88 | + # Add vertical box layout to widget |
| 89 | + self.setLayout(self.vboxLayout) |
| 90 | + |
| 91 | + def on_show_dialog_model(self): |
| 92 | + # Create dialog object |
| 93 | + dialog = Dialog(parent=self) |
83 | 94 |
|
84 | | - self.custom_dialog_modeless = None |
85 | | - self.name = '' |
86 | | - self.age = 0 |
87 | | - |
88 | | - # Set minimum window size |
89 | | - self.setMinimumSize(150, 100) |
90 | | - self.setWindowTitle('Custom dialog') |
91 | | - |
92 | | - self.setWindowFlags(self.windowFlags() & Qt.CustomizeWindowHint) |
93 | | - self.setWindowFlags(self.windowFlags() & ~Qt.WindowMinMaxButtonsHint) |
94 | | - |
95 | | - # Create push buttons |
96 | | - show_dialog_model = QPushButton('Model dialog', self) |
97 | | - show_dialog_model.clicked.connect(self.dialog_model_show) |
98 | | - show_dialog_model.setFixedWidth(130) |
99 | | - show_dialog_modeless = QPushButton('Modeless dialog', self) |
100 | | - show_dialog_modeless.clicked.connect(self.dialog_modeless_show) |
101 | | - show_dialog_modeless.setFixedWidth(130) |
102 | | - |
103 | | - # Add button to window |
104 | | - hbox = QHBoxLayout() |
105 | | - hbox.addWidget(show_dialog_model) |
106 | | - hbox.addWidget(show_dialog_modeless) |
107 | | - |
108 | | - # Create quit button |
109 | | - dialog_buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok) |
110 | | - dialog_buttons.button(QDialogButtonBox.StandardButton.Ok).setText('Quit') |
111 | | - dialog_buttons.accepted.connect(QApplication.quit) |
112 | | - |
113 | | - # Add model buttons and quit button to layout |
114 | | - vbox = QVBoxLayout() |
115 | | - vbox.addLayout(hbox) |
116 | | - vbox.addWidget(dialog_buttons) |
117 | | - |
118 | | - # Add layout to window |
119 | | - self.setLayout(vbox) |
120 | | - |
121 | | - def dialog_model_show(self): |
122 | | - # Create custom model dialog |
123 | | - custom_dialog_model = CustomDialog(parent=self, |
124 | | - title='Custom Model Dialog', |
125 | | - name=self.name, |
126 | | - age=self.age) |
127 | | - |
128 | | - # Wait until True (Ok / accepted) or False (Cancel / rejected) clicked |
129 | | - if custom_dialog_model.exec(): |
130 | | - # Get results from dialog |
131 | | - self.name = custom_dialog_model.get_name() |
132 | | - self.age = custom_dialog_model.get_age() |
133 | | - self.show_results(name=self.name, age=self.age) |
134 | | - |
135 | | - def dialog_modeless_show(self): |
136 | | - # Create custom modeless dialog |
137 | | - if not self.custom_dialog_modeless: |
138 | | - self.custom_dialog_modeless = CustomDialog(parent=self, |
139 | | - title='Custom Modeless Dialog', |
140 | | - name=self.name, |
141 | | - age=self.age) |
142 | | - self.custom_dialog_modeless.accepted.connect(self.dialog_modeless_close) |
143 | | - self.custom_dialog_modeless.rejected.connect(self.dialog_modeless_reject) |
144 | | - |
145 | | - # Show modeless dialog (show() returns immediately) |
146 | | - self.custom_dialog_modeless.show() |
147 | | - |
148 | | - def dialog_modeless_close(self): |
149 | | - # Get results from dialog |
150 | | - self.name = self.custom_dialog_modeless.get_name() |
151 | | - self.age = self.custom_dialog_modeless.get_age() |
152 | | - self.show_results(name=self.name, age=self.age) |
153 | | - |
154 | | - # Destroy object |
155 | | - self.custom_dialog_modeless = None |
156 | | - |
157 | | - def dialog_modeless_reject(self): |
158 | | - # Destroy object |
159 | | - self.custom_dialog_modeless = None |
160 | | - |
161 | | - def show_results(self, name, age): |
162 | | - # Show results in information message box |
163 | | - QMessageBox.information(self, |
164 | | - 'Result', |
165 | | - "Hi {}! \nYou are {} years old.".format(name, age), |
166 | | - QMessageBox.StandardButton.Ok) |
| 95 | + # Show dialog model and wait for result to display in an information message box |
| 96 | + if dialog.exec() == QDialog.Accepted: |
| 97 | + QMessageBox.information(self, "Dialog", f"Hello {dialog.editName.text()}!") |
| 98 | + else: |
| 99 | + QMessageBox.information(self, "Dialog", "Clicked cancel") |
167 | 100 |
|
168 | 101 |
|
169 | 102 | def main(): |
170 | 103 | app = QApplication(sys.argv) |
171 | | - main_window = MainWindow() |
172 | | - main_window.show() |
| 104 | + window = Window() |
| 105 | + window.show() |
173 | 106 | sys.exit(app.exec()) |
174 | 107 |
|
175 | 108 |
|
176 | | -if __name__ == '__main__': |
| 109 | +if __name__ == "__main__": |
177 | 110 | main() |
0 commit comments