GUI学习之二十八—QMessageBox

摘要:
今天,让我们了解一下QMessageBox。QMessageBox主要用于通知用户或请求用户提问并接收回复。从案例中可以看出,这是mb=QMessageBoxmb。setTextmb。setCheckBoxmb。setInformativeTextmb。setDetailedTextmb show()的效果是正确的。另外一点是,如果我们不想让文本显示支持富文本格式,我们也可以定义QMessageBox。setTextFormat#type:'Qt。TextFormat'PlainText=…#type:'Qt。TextFormat'RichText=…#type:'Qt。TextFormat'自动图文集=…#type:'Qt。TextFormat“The TextFormat”设置位置也不是必需的。2.按钮的定义有些复杂,可以分为许多类型。让我们一步一步来看看a。按钮的角色在对话框中有角色划分。

  

今天来学习下QMessageBox。

QMessageBox主要用来通知用户或者请求用户提问和接收应答一个模态对话框。

一.对话框的构成

GUI学习之二十八—QMessageBox第1张

图标是有标准图标的,可以直接调用。

我们声明的消息框,初始状态都是模态的(阻塞程序,这里就不演示了),如果想把它变成非模态的,可以直接设置

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()

出来的效果就是这样的

GUI学习之二十八—QMessageBox第2张

  其实这里没有演示,提示文本也是支持富文本的。

  有些时候在提示文本位置还有一个复选按钮(类似于下次不再提示的功能),是这么实现的

QMessageBox.setCheckBox()

在上面的效果加个案例

mb = QMessageBox(self)
mb.setText('<h2>文件已被修改</h2>'
           '<h3>是否保存</h3>')
mb.setCheckBox(QCheckBox('下次不再提示',mb))

mb.setInformativeText('点击是保存文件')
mb.show()

效果:

GUI学习之二十八—QMessageBox第3张

  还有展示详情文本

QMessageBox.setDetailedText()

详情文本设置后会有个显示详细内容的按钮,点击按钮会显示详情。可以从案例中看出来,这个是不支持富文本的

mb = QMessageBox(self)
mb.setText('<h2>文件已被修改</h2>'
           '<h3>是否保存</h3>')
mb.setCheckBox(QCheckBox('下次不再提示',mb))

mb.setInformativeText('点击是保存文件')
mb.setDetailedText('<h3>详情文本</h3>')
mb.show()

效果

GUI学习之二十八—QMessageBox第4张

  对了,补充一点,如果我们不想让文本显示支持富文本格式也是可以定义的

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所对应的枚举值)下面的表就给出了各种按键所对应的枚举值。

ConstantValueDescription
QDialogButtonBox.Ok0x00000400An "OK" button defined with the AcceptRole.
QDialogButtonBox.Open0x00002000A "Open" button defined with the AcceptRole.
QDialogButtonBox.Save0x00000800A "Save" button defined with the AcceptRole.
QDialogButtonBox.Cancel0x00400000A "Cancel" button defined with the RejectRole.
QDialogButtonBox.Close0x00200000A "Close" button defined with the RejectRole.
QDialogButtonBox.Discard0x00800000A "Discard" or "Don't Save" button, depending on the platform, defined with the DestructiveRole.
QDialogButtonBox.Apply0x02000000An "Apply" button defined with the ApplyRole.
QDialogButtonBox.Reset0x04000000A "Reset" button defined with the ResetRole.
QDialogButtonBox.RestoreDefaults0x08000000A "Restore Defaults" button defined with the ResetRole.
QDialogButtonBox.Help0x01000000A "Help" button defined with the HelpRole.
QDialogButtonBox.SaveAll0x00001000A "Save All" button defined with the AcceptRole.
QDialogButtonBox.Yes0x00004000A "Yes" button defined with the YesRole.
QDialogButtonBox.YesToAll0x00008000A "Yes to All" button defined with the YesRole.
QDialogButtonBox.No0x00010000A "No" button defined with the NoRole.
QDialogButtonBox.NoToAll0x00020000A "No to All" button defined with the NoRole.
QDialogButtonBox.Abort0x00040000An "Abort" button defined with the RejectRole.
QDialogButtonBox.Retry0x00080000A "Retry" button defined with the AcceptRole.
QDialogButtonBox.Ignore0x00100000An "Ignore" button defined with the AcceptRole.
QDialogButtonBox.NoButton0x00000000An invalid button.

免责声明:文章转载自《GUI学习之二十八—QMessageBox》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇大数据培训班 cloudera公司讲师面对面授课 CCDH CCAH CCPoracle怎么建立本地连接下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

DOM操作元素

DOM 操作元素 JavaScript的DOM操作可以改变网页内容、结构和样式。我们可以利用DOM操作元素来改变元素里面的内容、属性等。 DOM操作元素: 一、操作元素:(一)innerText 、(二)innerHTML 二、操作常见元素属性:src、href、title、alt等; 三、操作表单元素属性:type、value、disabled等; 四...

Xcode插件管理

 在使用Xcode的时候,公司同事使用/// 和//TODO 就能打出很多注释信息。虽然他们帮忙给我也装了,但是我却不知道怎么弄的。今天在家无聊,过来自己实践了一把。  so easy。 1.我使用的是Package Manager for Xcode。这时,我们要使用要将它下载下来,并且装入到Xcode里面。 原本以为会很繁琐,结果一句话就搞定了。 官网...

[转载]iOS 开发中为什么更新UI都要放在主线程中?

原因有2个: 1、在子线程中是不能进行UI 更新的,而可以更新的结果只是一个幻像:因为子线程代码执行完毕了,又自动进入到了主线程,执行了子线程中的UI更新的函数栈,这中间的时间非常的短,就让大家误以为分线程可以更新UI。如果子线程一直在运行,则子线程中的UI更新的函数栈 主线程无法获知,即无法更新 2、只有极少数的UI能,因为开辟线程时会获取当前环境,如点...

ucGUI的视窗管理回调机制学习

要熟悉窗口的回调机制,重点理解回调函数作用,消息传递机制。 uC/GUI的窗口管理是个单独的软件,不是uC/GUI的基本组成部分。代码见\uCGUI\GUI\WM。当使用uC/GUI窗口管理时,任何能显示在显示终端上的内容都包含在一个窗口里面,这个窗口是LCD屏幕上的一个给用户画图或显示目标的区域。窗口能够是任何尺寸的,能够一次在屏幕上显示多个窗口,也能够...

WPF知识点全攻略11- 命令(Command)

先看一下命令的简单使用: <Window.CommandBindings> <CommandBinding Command="ApplicationCommands.Cut" CanExecute="CutCommand_CanExecute" Executed="CutCommand_Executed" />...

抽象工厂模式(C++)

#define win 0 #define mac 1 #include <iostream> using namespace std; class button { public: button(){} virtual ~button(){} virtual void showbutton()=0; }; clas...