今天來學習下QMessageBox。
QMessageBox主要用來通知用戶或者請求用戶提問和接收應答一個模態對話框。
一.對話框的構成
圖標是有標准圖標的,可以直接調用。
我們聲明的消息框,初始狀態都是模態的(阻塞程序,這里就不演示了),如果想把它變成非模態的,可以直接設置
mb = QMessageBox(self) # mb.setModal(False) #方法1 mb.setWindowModality(Qt.NonModal) #方法2 mb.show()
上面兩個方法都是可以的,但是必須是窗口級別的控件(show()方法調用的,open和exec是不可以的。)
二.內容展示
1.圖標
標准圖標的展示方法
QMessageBox.setIcon(self, a0: 'QMessageBox.Icon') #圖標枚舉值type: 'QMessageBox.Icon' NoIcon = ... # 無圖標 Information = ... # 信息圖標(也可以表示消息無異常) Warning = ... # 警告圖標 Critical = ... # 嚴重警告圖標 Question = ... # 提問圖標
此外還可以添加自定義圖標
QMessageBox.setIconPixmap(self, a0: QtGui.QPixmap)
2.內容
消息框的內容是支持富文本的,我們可以用setText()函數直接設置其提示內容,此外還有一個setInformativeText(),是用來顯示提示文本的
mb = QMessageBox(self) mb.setText('<h2>文件已被修改</h2>' '<h3>是否保存</h3>') mb.setInformativeText('點擊是保存文件') mb.show()
出來的效果就是這樣的
其實這里沒有演示,提示文本也是支持富文本的。
有些時候在提示文本位置還有一個復選按鈕(類似於下次不再提示的功能),是這么實現的
QMessageBox.setCheckBox()
在上面的效果加個案例
mb = QMessageBox(self) mb.setText('<h2>文件已被修改</h2>' '<h3>是否保存</h3>') mb.setCheckBox(QCheckBox('下次不再提示',mb)) mb.setInformativeText('點擊是保存文件') mb.show()
效果:
還有展示詳情文本
QMessageBox.setDetailedText()
詳情文本設置后會有個顯示詳細內容的按鈕,點擊按鈕會顯示詳情。可以從案例中看出來,這個是不支持富文本的
mb = QMessageBox(self) mb.setText('<h2>文件已被修改</h2>' '<h3>是否保存</h3>') mb.setCheckBox(QCheckBox('下次不再提示',mb)) mb.setInformativeText('點擊是保存文件') mb.setDetailedText('<h3>詳情文本</h3>') mb.show()
效果
對了,補充一點,如果我們不想讓文本顯示支持富文本格式也是可以定義的
QMessageBox.setTextFormat(self, a0: QtCore.Qt.TextFormat) # type: 'Qt.TextFormat' PlainText = ... # type: 'Qt.TextFormat' RichText = ... # type: 'Qt.TextFormat' AutoText = ... # type: 'Qt.TextFormat'
設置的位置也是沒有強制要求的
2.按鈕
按鈕的定義有些復雜,分很多種,我們一步步來看
a.按鈕的角色
按鈕在對話框中是有角色分工的(ButtonRole)。在了解按鈕的配置時我們要先了解按鈕有哪些角色分工
# 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'
如果我們需要自定義個按鈕,這個按鈕是起了哪個角色的作用,我們就可以給出相應的定義
a.添加移除自定義按鈕
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)
如果想要移除按鈕的話可以直接刪除按鈕控件,要注意的是后面兩個添加自定義按鈕的方法是有個返回值的,因為我們並不是先實例一個按鈕在添加在對話框中而是直接在添加的時候定義,那么添加的這個按鈕控件作為返回值被返回。
b.默認按鈕設置
有些時候我們想彈出的對話框某一個按鈕是在焦點上的,那么我們就可以直接通過鍵盤的回車鍵對其進行操作,那么就用到下面的方法:設置默認按鈕
標准按鈕定義
QMessageBox.setDefaultButton(self, button: QPushButton) QMessageBox.setDefaultButton(self, button: 'QMessageBox.StandardButton')
c.關聯退出按鈕(鍵盤Esc)
我們可以把鍵盤Esc鍵關聯給某個按鈕,當鍵盤Esc鍵被按下時對話框退出,並且關聯的按鈕會觸發clicked事件。
QMessageBox.setEscapeButton(self, button: QAbstractButton)
d.按鈕獲取
正常情況下對話框都是有幾個按鈕的,如果我們可以通過下面的代碼獲取一個按鈕
QMessageBox.button(self, which: 'QMessageBox.StandardButton') -> QAbstractButton
這種情況只適用於標准的按鈕控件
也可以通過下面的方法獲取按鈕的角色
QMessageBox.buttonRole(self, button: QAbstractButton) -> 'QMessageBox.ButtonRole'
最后,我們也可以通過一個信號來獲取被點擊的按鈕
QMessageBox.buttonClicked(self, button: QAbstractButton)
這個信號是可以有參數傳遞的(按鈕對象)。
3.文本交互標志
默認情況下消息框里的文本是不能被選中的,詳情是可以選中並復制。我們可以通過下面的方法改變其狀態
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'
這里的枚舉值比較多,就不一一演示了。
4.主要靜態方法
有些靜態方法是很好用的,我們可以直接彈出個消息框用來給出提示信息。也就是說直接給封裝好的消息界面,只要把關鍵的提示字在初始化的時候定義,就可以直接用了。
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' = ...)
這些對話框方法基本一樣,就是圖標不同。並且這種方法是有返回值的(返回值為每個StandardButton所對應的枚舉值)下面的表就給出了各種按鍵所對應的枚舉值。
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. |