1 / 23

Rapid GUI Programming with Python and Qt

Rapid GUI Programming with Python and Qt. Dialogs By Raed S. Rasheed. Dialogs.

olisa
Download Presentation

Rapid GUI Programming with Python and Qt

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Rapid GUI Programmingwith Python and Qt Dialogs By Raed S. Rasheed

  2. Dialogs Almost every GUI application has at least one dialog, and the majority of GUI applications have one main window with dozens or scores of dialogs. Dialogs can be used to make announcements that are too important to put in the status bar or into a log file. In such cases, they typically just have a label for the text and an OK button for the user to press when they’ve read the message. Mostly, dialogs are used to ask users questions. Some are simple and need just a yes or no answer.

  3. Dialogs One way to classify dialogs is by their “intelligence”, where they may be “dumb”, “standard”, or “smart”, depending on how much knowledge about the application’s data is built into them. In addition to an intelligence classification, dialogs can also be categorized by their modality. An application modal dialog is a dialog that, once invoked, is the only part of an application that the user can interact with. Until the user closes the dialog, they cannot use the rest of the application. The user is, of course, free to interact with other applications, for example, by clicking one to give it the focus.

  4. Dialogs A window modal dialog is one that works in a similar way to an application modal dialog, except that it only prevents interaction with its parent window, parent’s parent window, and so on up to the top-level parent, as well as the parent windows’ sibling windows. The opposite of a modal dialog is a modeless dialog. When a modeless dialog is invoked, the user can interact with the dialog, and with the rest of the application.

  5. Dumb Dialogs The Pen Properties dialog

  6. Dumb Dialogs defsetPenProperties(self): dialog = PenPropertiesDlg(self) dialog.widthSpinBox.setValue(self.width) dialog.beveledCheckBox.setChecked(self.beveled) dialog.styleComboBox.setCurrentIndex( dialog.styleComboBox.findText(self.style)) if dialog.exec_(): self.width = dialog.widthSpinBox.value() self.beveled = dialog.beveledCheckBox.isChecked() self.style = unicode(dialog.styleComboBox.currentText()) self.updateData()

  7. Dumb Dialogs class PenPropertiesDlg(QDialog): def __init__(self, parent=None): super(PenPropertiesDlg, self).__init__(parent) widthLabel = QLabel("&Width:") self.widthSpinBox = QSpinBox() widthLabel.setBuddy(self.widthSpinBox) self.widthSpinBox.setAlignment(Qt.AlignRight|Qt.AlignVCenter) self.widthSpinBox.setRange(0, 24) self.beveledCheckBox = QCheckBox("&Beveled edges")

  8. Dumb Dialogs styleLabel= QLabel("&Style:") self.styleComboBox = QComboBox() styleLabel.setBuddy(self.styleComboBox) self.styleComboBox.addItems(["Solid", "Dashed", "Dotted", "DashDotted", "DashDotDotted"]) okButton = QPushButton("&OK") cancelButton = QPushButton("Cancel")

  9. Dumb Dialogs buttonLayout = QHBoxLayout() buttonLayout.addStretch() buttonLayout.addWidget(okButton) buttonLayout.addWidget(cancelButton) layout = QGridLayout() layout.addWidget(widthLabel, 0, 0) layout.addWidget(self.widthSpinBox, 0, 1) layout.addWidget(self.beveledCheckBox, 0, 2) layout.addWidget(styleLabel, 1, 0) layout.addWidget(self.styleComboBox, 1, 1, 1, 2) layout.addLayout(buttonLayout, 2, 0, 1, 3) self.setLayout(layout)

  10. Dumb Dialogs The Pen Properties dialog’s layout

  11. Dumb Dialogs self.connect(okButton, SIGNAL("clicked()"), self, SLOT("accept()")) self.connect(cancelButton, SIGNAL("clicked()"), self, SLOT("reject()")) self.setWindowTitle("Pen Properties")

  12. Dumb Dialogs Selected Layout Methods

  13. Standard Dialogs One key advantage of standard dialogs is that the caller does not need to know about their implementation, only how to set the initial values, and how to get the resultant values if the user clicked OK. Another advantage, at least for modal standard dialogs, is that the user cannot interact with the dialog’s parent windows and their sibling windows, so the relevant parts of the application’s state will probably not change behind the dialog’s back. The main drawback of using a standard dialog is most apparent when it must handle lots of different data items, since all the items must be fed into the dialog and the results retrieved on each invocation, and this may involve many lines of code.

  14. Standard Dialogs The modal Set Number Format dialog in context

  15. Standard Dialogs Modal OK/Cancel-Style Dialogs def setNumberFormat1(self): dialog = numberformatdlg1.NumberFormatDlg(self.format, self) if dialog.exec_(): self.format = dialog.numberFormat() self.refreshTable()

  16. Standard Dialogs class NumberFormatDlg(QDialog): def __init__(self, format, parent=None): super(NumberFormatDlg, self).__init__(parent) thousandsLabel = QLabel("&Thousands separator") self.thousandsEdit = QLineEdit(format["thousandsseparator"]) thousandsLabel.setBuddy(self.thousandsEdit) decimalMarkerLabel = QLabel("Decimal &marker") self.decimalMarkerEdit = QLineEdit(format["decimalmarker"]) decimalMarkerLabel.setBuddy(self.decimalMarkerEdit) decimalPlacesLabel = QLabel("&Decimal places")

  17. Standard Dialogs self.decimalPlacesSpinBox= QSpinBox() decimalPlacesLabel.setBuddy(self.decimalPlacesSpinBox) self.decimalPlacesSpinBox.setRange(0, 6) self.decimalPlacesSpinBox.setValue(format["decimalplaces"]) self.redNegativesCheckBox = QCheckBox("&Red negative numbers") self.redNegativesCheckBox.setChecked(format["rednegatives"]) buttonBox = QDialogButtonBox(QDialogButtonBox.Ok| QDialogButtonBox.Cancel)

  18. Standard Dialogs Selected QDialogButtonBox Methods and Signals

  19. Smart Dialogs The main benefit of using a smart modeless dialog is seen at the point of use. When the dialog is created, it is given references to the calling form’s data structures so that the dialog can update the data structures directly with no further code required at the call point. The downsides are that the dialog must have knowledge of the calling form’s data structures so that it correctly reflects the data values into its widgets and only applies changes that are valid, and that, being modeless, there is a risk of the data the dialog depends on being changed from under it if the user interacts with some other part of the application.

  20. Smart Dialogs Modeless Apply/Close-Style Dialogs Superficially, the only difference between the modeless and the modal versions of the dialog is the button text. However, there are two other important differences: The calling form’s method creates and invokes the dialog differently, and the dialog must make sure it is deleted, not just hidden, when it is closed. Let us begin by looking at how the dialog is invoked.

  21. Smart Dialogs defsetNumberFormat2(self): dialog = numberformatdlg2.NumberFormatDlg(self.format, self) self.connect(dialog, SIGNAL("changed"), self.refreshTable) dialog.show()

  22. Smart Dialogs Modeless “Live” Dialogs We could create this dialog in exactly the same way as the previous dialog, but we will instead demonstrate a different approach. Rather than creating the dialog when it is needed and then destroying it, creating and destroying on every use, we will create it just once, the first time it is needed, and then hide it when the user is finished with it, showing and hiding on every use.

  23. Smart Dialogs defsetNumberFormat3(self): if self.numberFormatDlg is None: self.numberFormatDlg= numberformatdlg3.NumberFormatDlg( self.format, self.refreshTable, self) self.numberFormatDlg.show() self.numberFormatDlg.raise_() self.numberFormatDlg.activateWindow()

More Related