with Python and Qt Dialogs By Raed S Rasheed 1 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 ID: 264270
Download Presentation The PPT/PDF document "Rapid GUI Programming" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Rapid GUI Programmingwith Python and Qt
DialogsByRaed S. Rasheed
1Slide2
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.
2Slide3
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.
3Slide4
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.4Slide5
Dumb Dialogs
The
Pen Properties dialog
5Slide6
Dumb Dialogs
def setPenProperties(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()
6Slide7
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")7Slide8
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")
8Slide9
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)
9Slide10
Dumb Dialogs
The
Pen Properties dialog’s layout
10Slide11
Dumb Dialogs
self.connect(okButton, SIGNAL("clicked()"),
self
, SLOT("accept()"))
self.connect(cancelButton, SIGNAL("clicked()"), self, SLOT("reject()"))self.setWindowTitle("Pen Properties")11Slide12
Dumb Dialogs
Selected Layout Methods
12Slide13
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.
13Slide14
Standard Dialogs
The
modal Set Number Format dialog in context
14Slide15
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
()
15Slide16
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
")
16Slide17
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)17Slide18
Standard Dialogs
Selected
QDialogButtonBox
Methods and Signals
18Slide19
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.
19Slide20
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.20Slide21
Smart Dialogs
def setNumberFormat2(self):dialog = numberformatdlg2.NumberFormatDlg(
self.format
, self)
self.connect(dialog, SIGNAL("changed"), self.refreshTable)dialog.show()21Slide22
Smart Dialogs
Modeless “Live” DialogsWe 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.22Slide23
Smart Dialogs
def setNumberFormat3(self):if
self.numberFormatDlg
is None:
self.numberFormatDlg = numberformatdlg3.NumberFormatDlg( self.format, self.refreshTable, self)
self.numberFormatDlg.show
()
self.numberFormatDlg.raise
_()
self.numberFormatDlg.activateWindow
()
23