Today to learn under QMessageBox.
QMessageBox mainly used to notify the user or ask the user questions and receive responses a modal dialog.
A. Dialog constitution
Icons are standard icons, you can directly call.
We declare the message box, the initial state is modal (blocking program, there is not demonstrated), if it wants to become a non-modal, you can directly set
= MB a QMessageBox (Self) # mb.setModal (False) Method #. 1 mb.setWindowModality (Qt.NonModal) # Method 2 mb.show ()
The above two methods are possible, but must be a window-level controls (show () method call, open and exec is not.)
II. Content display
1. icon
Standard method for displaying icons
QMessageBox.setIcon (Self, A0: ' QMessageBox.Icon ' ) # Icon enumerated value of the type: 'QMessageBox.Icon' NoIcon = ... # No icon Information = ... # information icon (can also be a message indicating no abnormality) = ... warning # warning icon critical = ... # serious warning icon question = ... # question icon
In addition you can also add a custom icon
QMessageBox.setIconPixmap(self, a0: QtGui.QPixmap)
2. Content
The content of the message box is to support rich text, we can () function directly to set its prompt content with setText, in addition to a setInformativeText (), is used to display the text prompt
= MB a QMessageBox (Self) mb.setText ( ' <H2> file has been modified </ H2> ' ' <H3> to save </ H3> ' ) mb.setInformativeText ( ' click Save File is ' ) mb.show ( )
Out of the effect is such
In fact, there is no demonstration, suggesting that the text also supports rich text.
Sometimes there is a position in the prompt text check button (similar to no longer prompt the next function) is implemented so
QMessageBox.setCheckBox()
In the above effect plus cases
= MB a QMessageBox (Self) mb.setText ( ' <H2> file has been modified </ H2> ' ' <H3> to save </ H3> ' ) mb.setCheckBox (QCheckBoxes ( ' Save not prompt ' , MB )) mb.setInformativeText ( ' click Yes to save the file ' ) mb.show ()
effect:
There are details to show text
QMessageBox.setDetailedText()
After the details of the text set will have a display button details, click the button displays the details. It can be seen from the case, this does not support rich text
= MB a QMessageBox (Self) mb.setText ( ' <H2> file has been modified </ H2> ' ' <H3> to save </ H3> ' ) mb.setCheckBox (QCheckBoxes ( ' Save not prompt ' , MB )) mb.setInformativeText ( ' click Yes to save the file ' ) mb.setDetailedText ( ' <h3> details text </ h3> ' ) mb.show ()
effect
Yes, add that, if we do not want the text display can support rich text format is also defined
QMessageBox.setTextFormat(self, a0: QtCore.Qt.TextFormat) # type: 'Qt.TextFormat' PlainText = ... # type: 'Qt.TextFormat' RichText = ... # type: 'Qt.TextFormat' AutoText = ... # type: 'Qt.TextFormat'
Location setting is not mandatory
2. Button
Custom button somewhat complex, very many, we look at a step by step
a. the role of a button
Button in the dialog box is the role of division of labor (ButtonRole). When we understand the Configure button button must first understand what the role of division of labor
# type: 'QMessageBox.ButtonRole' InvalidRole = ... # type: 'QMessageBox.ButtonRole' AcceptRole = ... # type: 'QMessageBox.ButtonRole' RejectRole = ... # type: 'QMessageBox.ButtonRole' DestructiveRole = ... # type: 'QMessageBox.ButtonRole' ActionRole = ... # type: 'QMessageBox.ButtonRole' HelpRole = ... # type: 'QMessageBox.ButtonRole' YesRole = ... # type: 'QMessageBox.ButtonRole' NoRole = ... # type: 'QMessageBox.ButtonRole' ResetRole = ... # type: 'QMessageBox.ButtonRole' ApplyRole = ... # type: 'QMessageBox.ButtonRole'
If we need to customize a button that played a role in which role, we can give the corresponding definitions
a. Add remove custom buttons
QMessageBox.addButton(self, button: QAbstractButton, role: 'QMessageBox.ButtonRole') QMessageBox.addButton(self, text: str, role: 'QMessageBox.ButtonRole')-> QPushButton QMessageBox.addButton(self, button: 'QMessageBox.StandardButton')-> QPushButton QMessageBox.removeButton(self, button: QAbstractButton)
If you want to remove the button, then you can directly delete button controls, it should be noted that the latter two ways to add custom buttons that have a return value, because we are not in the first instance to add a button in the dialog but directly when adding the definition, then add this button control is returned as the return value.
b. The default button settings
Sometimes we want the dialog box a button is in focus, then we can operate directly through the Enter key on the keyboard to its, then use the following method: Set the default button
Standard button to define
QMessageBox.setDefaultButton(self, button: QPushButton) QMessageBox.setDefaultButton(self, button: 'QMessageBox.StandardButton')
c. associated with the Exit button (keyboard Esc)
We can associate the keyboard Esc key to a button, when the keyboard key is pressed Esc to exit the dialog box and clicked a button triggers the associated event.
QMessageBox.setEscapeButton(self, button: QAbstractButton)
d. button to get
Under normal circumstances, there are several buttons of the dialog box, if we can get the following code for a button
QMessageBox.button(self, which: 'QMessageBox.StandardButton') -> QAbstractButton
This applies only to the standard button controls
You can also get the role of a button by the following method
QMessageBox.buttonRole(self, button: QAbstractButton) -> 'QMessageBox.ButtonRole'
Finally, we can also get the button is clicked by a signal
QMessageBox.buttonClicked(self, button: QAbstractButton)
This signal can (button object) parameters passed.
3. Text interaction logo
The default message box in the case of the text is not selected, the details can be selected and copied. We can change its state by the following methods
QMessageBox.setTextInteractionFlags(self, flags: typing.Union[QtCore.Qt.TextInteractionFlags, QtCore.Qt.TextInteractionFlag]) # type: 'Qt.TextInteractionFlag' NoTextInteraction = ... # type: 'Qt.TextInteractionFlag' TextSelectableByMouse = ... # type: 'Qt.TextInteractionFlag' TextSelectableByKeyboard = ... # type: 'Qt.TextInteractionFlag' LinksAccessibleByMouse = ... # type: 'Qt.TextInteractionFlag' LinksAccessibleByKeyboard = ... # type: 'Qt.TextInteractionFlag' TextEditable = ... # type: 'Qt.TextInteractionFlag' TextEditorInteraction = ... # type: 'Qt.TextInteractionFlag' TextBrowserInteraction = ... # type: 'Qt.TextInteractionFlag'
Here enumeration value more, not one demonstration.
4. The main static methods
Some static method is very easy to use, we can directly pop up a message box to prompt information. That is good news interface directly to the package, as long as the key word prompts defined at initialization, can be directly used.
QMessageBox.about(parent: QWidget, caption: str, text: str) #提示消息对话框 QMessageBox.question(parent: QWidget, title: str, text: str, buttons: typing.Union['QMessageBox.StandardButtons', 'QMessageBox.StandardButton'] = ..., defaultButton: 'QMessageBox.StandardButton' = ...) QMessageBox.warning(parent: QWidget, title: str, text: str, buttons: typing.Union['QMessageBox.StandardButtons', 'QMessageBox.StandardButton'] = ..., defaultButton: 'QMessageBox.StandardButton' = ...)
These dialog boxes are basically the same method is different icons. And this is the method that returns a value (return value for each enumeration value corresponding StandardButton) The following table shows the enumeration values to corresponding various keys.
Constant | Value | Description |
---|---|---|
QDialogButtonBox.Ok | 0x00000400 | An "OK" button defined with the AcceptRole. |
QDialogButtonBox.Open | 0x00002000 | A "Open" button defined with the AcceptRole. |
QDialogButtonBox.Save | 0x00000800 | A "Save" button defined with the AcceptRole. |
QDialogButtonBox.Cancel | 0x00400000 | A "Cancel" button defined with the RejectRole. |
QDialogButtonBox.Close | 0x00200000 | A "Close" button defined with the RejectRole. |
QDialogButtonBox.Discard | 0x00800000 | A "Discard" or "Don't Save" button, depending on the platform, defined with the DestructiveRole. |
QDialogButtonBox.Apply | 0x02000000 | An "Apply" button defined with the ApplyRole. |
QDialogButtonBox.Reset | 0x04000000 | A "Reset" button defined with the ResetRole. |
QDialogButtonBox.RestoreDefaults | 0x08000000 | A "Restore Defaults" button defined with the ResetRole. |
QDialogButtonBox.Help | 0x01000000 | A "Help" button defined with the HelpRole. |
QDialogButtonBox.SaveAll | 0x00001000 | A "Save All" button defined with the AcceptRole. |
QDialogButtonBox.Yes | 0x00004000 | A "Yes" button defined with the YesRole. |
QDialogButtonBox.YesToAll | 0x00008000 | A "Yes to All" button defined with the YesRole. |
QDialogButtonBox.No | 0x00010000 | A "No" button defined with the NoRole. |
QDialogButtonBox.NoToAll | 0x00020000 | A "No to All" button defined with the NoRole. |
QDialogButtonBox.Abort | 0x00040000 | An "Abort" button defined with the RejectRole. |
QDialogButtonBox.Retry | 0x00080000 | A "Retry" button defined with the AcceptRole. |
QDialogButtonBox.Ignore | 0x00100000 | An "Ignore" button defined with the AcceptRole. |
QDialogButtonBox.NoButton | 0x00000000 | An invalid button. |