当前位置: 首页 > news >正文

PyQt5桌面应用开发(5):对话框

本文目录

  • PyQt5桌面应用系列
  • 对话框
  • `QDialog`
    • `QDialog`的基本用法
    • 按钮组
  • `QMessageBox`
  • 综合展示的例子
  • 结论

PyQt5桌面应用系列

  • PyQt5桌面应用开发(1):需求分析
  • PyQt5桌面应用开发(2):事件循环
  • PyQt5桌面应用开发(3):并行设计
  • PyQt5桌面应用开发(4):界面设计
  • PyQt5桌面应用开发(5):对话框
  • [PyQt5桌面应用开发(7):文本编辑+语法高亮与行号](https://withstand.blog.csdn.net/article/details/130486145

对话框

对话框在GUI中是一个常见的组件,用于和用户进行交互。这个交互分为两个部分。

  • 为用户提示信息,获取用户的注意;
  • 为用户提供输入信息的界面。

PyQt5提供了一些内置的对话框,也可以自定义对话框。所有的PyQt5对话框都是QDialog的子类。

QDialog

QDialog的基本用法

QDialog定义的信号有:

  • accepted():用户点击了对话框的确认按钮;
  • finished(int result):用户点击了对话框的确认或取消按钮,resultQDialog的返回值,QDialog的返回值是QDialog.AcceptedQDialog.Rejected
  • rejected():用户点击了对话框的取消按钮。

还包括从QWidget继承的信号:

  • updateMicroFocus():更新对话框的焦点。
  • windowTitleChanged(const QString &title):对话框的标题发生了变化。
  • customContextMenuRequested(const QPoint &pos):用户请求了自定义的上下文菜单。
  • windowIconChanged(const QIcon &icon):对话框的图标发生了变化。
  • windowIconTextChanged(const QString &iconText):对话框的图标文字发生了变化。
  • windowModalityChanged(Qt::WindowModality windowModality):对话框的模态性发生了变化。
  • windowOpacityChanged(qreal level):对话框的透明度发生了变化。
  • windowStateChanged(Qt::WindowStates oldState, Qt::WindowStates newState):对话框的状态发生了变化。

实际上,我们需要关注的就是accepted()rejected()信号。目前PyQt5推荐采用open()方法显示对话框,这种情况下,就应该连接finished信号来处理对话框的返回值。这一点应该引起注意,好多网上的例子都用exec()等方法来显示对话框,这不是PyQt5推荐的方法,官方文档中专门针对这个进行了说明。

Note: Avoid using this function; instead, use open(). Unlike exec(), open() is asynchronous, and does not spin an additional event loop. This prevents a series of dangerous bugs from happening (e.g. deleting the dialog’s parent while the dialog is open via exec()). When using open() you can connect to the finished() signal of QDialog to be notified when the dialog is closed.

注意:避免使用这个函数,而是使用open()。与exec()不同,open()是异步的,不会再启动一个事件循环。这样可以避免一系列的危险bug(例如,当对话框通过exec()打开时,删除对话框的父窗口)。当使用open()时,可以连接到QDialogfinished()信号,以便在对话框关闭时得到通知。

我们可以自己定义QPushButton,然后连接clicked信号到accept()reject()槽,这样就可以在点击按钮时关闭对话框。

按钮组

但是PyQt5还提供了一些默认的按钮组件。

  • QDialogButtonBox.Ok
  • QDialogButtonBox.Open
  • QDialogButtonBox.Save
  • QDialogButtonBox.Cancel
  • QDialogButtonBox.Close
  • QDialogButtonBox.Discard
  • QDialogButtonBox.Apply
  • QDialogButtonBox.Reset
  • QDialogButtonBox.RestoreDefaults
  • QDialogButtonBox.Help
  • QDialogButtonBox.SaveAll
  • QDialogButtonBox.Yes
  • QDialogButtonBox.YesToAll
  • QDialogButtonBox.No
  • QDialogButtonBox.Abort
  • QDialogButtonBox.Retry
  • QDialogButtonBox.Ignore
  • QDialogButtonBox.NoButton

典型的用法是:

QBtn = QDialogButtonBox.Ok | QDialogButtonBox.Cancel# connect buttons to slots to set result value of the dialog
self.buttonBox = QDialogButtonBox(QBtn)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)

QDialog是一个QWidget,所以可以使用通用的布局组件来放置所有的控件,并有所有属性操作,例如setWindowTitle()方法设置对话框的标题。QDialogButtonBox也按照需求放在布局中。

QMessageBox

为了方便展示信息,PyQt5提供了QMessageBox组件。QMessageBox提供了一些静态方法,可以直接调用,例如:

  • QMessageBox.about()
  • QMessageBox.aboutQt()
  • QMessageBox.critical()
  • QMessageBox.information()
  • QMessageBox.question()
  • QMessageBox.warning()
  • QMessageBox.error()

也可以创建一个QMessageBox实例,然后调用open()方法显示对话框。这时,可以用message_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)方法设置默认按钮。

这里有很多可以选的按钮,例如:

  • QMessageBox.Ok
  • QMessageBox.Open
  • QMessageBox.Save
  • QMessageBox.Cancel
  • QMessageBox.Close
  • QMessageBox.Discard
  • QMessageBox.Apply
  • QMessageBox.Reset
  • QMessageBox.RestoreDefaults
  • QMessageBox.Help
  • QMessageBox.SaveAll
  • QMessageBox.Yes
  • QMessageBox.YesToAll
  • QMessageBox.No
  • QMessageBox.NoToAll
  • QMessageBox.Abort
  • QMessageBox.Retry
  • QMessageBox.Ignore
  • QMessageBox.NoButton

QMessageBox的返回值对应于这些按钮,例如QMessageBox.YesQMessageBox.NoQMessageBox.Cancel等。

综合展示的例子

综合上面的例子,可以整一个综合的例子,包括QDialogQMessageBoxQDialogButtonBox等。

import sysfrom PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QHBoxLayout, QPushButton, QWidget, QVBoxLayout, \QTextEdit, QDialogButtonBox, QLabel, QMessageBoxclass EmptyDialog(QDialog):def __init__(self, parent=None):super(EmptyDialog, self).__init__(parent)self.setWindowTitle("BigEmptyDialog")self.resize(400, 300)class CustomDialog(QDialog):def __init__(self, parent=None):super(CustomDialog, self).__init__(parent)self.setWindowTitle("ButtonBox")QBtn = QDialogButtonBox.Ok | QDialogButtonBox.Cancel# connect buttons to slots to set result value of the dialogself.buttonBox = QDialogButtonBox(QBtn)self.buttonBox.accepted.connect(self.accept)self.buttonBox.rejected.connect(self.reject)self.layout = QVBoxLayout()message = QLabel("Something happened, is that OK?")self.layout.addWidget(message)self.layout.addWidget(self.buttonBox)self.setLayout(self.layout)def make_buttons(parent: QWidget, output: QTextEdit):button1 = QPushButton("Show EmptyDialog", parent)dlg1 = EmptyDialog(parent)button1.clicked.connect(dlg1.open)dlg1.finished.connect(lambda result: output.append(f"EmptyDialog finished."))button2 = QPushButton("Show CustomDialog", parent)dlg2 = CustomDialog(parent)button2.clicked.connect(dlg2.open)dlg2.finished.connect(lambda result: output.append(f"CustomDialog result: {result==QDialog.Accepted}"))button3 = QPushButton("MessageBox", parent)dlg3 = QMessageBox(parent)dlg3.setWindowTitle("I have a question!")dlg3.setText("This is a simple dialog")dlg3.setStandardButtons(QMessageBox.Yes | QMessageBox.No)dlg3.setIcon(QMessageBox.Question)dlg3.finished.connect(lambda result: output.append(f"MessageBox result: {result == QMessageBox.Yes}"))button3.clicked.connect(dlg3.open)bs = []for builtins, label in zip([QMessageBox.about,QMessageBox.critical,QMessageBox.information,QMessageBox.question,QMessageBox.warning, ], ["about", "critical", "information", "question", "warning"]):button = QPushButton(f"Show QMessageBox.{label}")button.clicked.connect(lambda checked: builtins(parent, f"QMessageBox.{label}", f"Is {label} look fine?"))bs.append(button)bs.extend([button1, button2, button3])return bsif __name__ == '__main__':app = QApplication(sys.argv)win = QMainWindow()win.setWindowTitle("Dialog buttons")# center widgetcenter = QWidget(win)border = QHBoxLayout()center.setLayout(border)text = QTextEdit(center)button_layout = QVBoxLayout()button_layout.setAlignment(Qt.AlignTop)buttons = make_buttons(center, text)for b in buttons:button_layout.addWidget(b)border.addLayout(button_layout)border.setStretch(0, 1)center_layout = QHBoxLayout()border.addLayout(center_layout)border.setStretch(1, 6)center_layout.addWidget(text)# Show the windowwin.setCentralWidget(center)win.setMinimumSize(1024, 768)win.setWindowIcon(QIcon(QPixmap("icon.png")))win.show()sys.exit(app.exec_())

结论

  1. QDialog是一个QWidget,所以可以制作任意复杂的对话框。
  2. QMessageBox提供了一个快速的实现。
  3. QDialogButtonBox提供了一组标准的按钮,可以方便的使用。
  4. QDialog及其子类会找其parent搜索Icon。
http://www.lryc.cn/news/64599.html

相关文章:

  • 整洁的代码
  • Redis集群常用命令及说明
  • 使用edge浏览器,白嫖ChatGPT的保姆级教程来了
  • 新人入职,都用这三招,让你安全度过试用期
  • 小程序上车,车载小程序的信息安全是否可靠?
  • 华为OD机试 - 识图谱新词挖掘(Python)
  • ( 数组和矩阵) 378. 有序矩阵中第 K 小的元素 ——【Leetcode每日一题】
  • HBase架构篇 - Hadoop家族的天之骄子HBase
  • STL及常用容器vector、list和deque的介绍
  • SpringBoot统一功能处理(统⼀⽤户登录权限验证、统⼀异常处理、统⼀数据格式封装)
  • 华为实习笔试复盘(1)配送站和客户问题
  • alibaba yalantingLibs struct_pack代码梳理
  • JavaWeb( 二 ) URL
  • Python斐波那契数列
  • 华为OD机试 - 模拟商场优惠打折(Python)
  • 【JAVA程序设计】(C00132)基于SSM的固定资产管理系统
  • 简单的无理函数的不定积分
  • 《国际联网安全保护管理办法》
  • Redis常用命令
  • 功能齐全的 DIY ESP32 智能手表设计之原理图讲解二
  • 烦恼的高考志愿
  • 【地铁上的设计模式】--结构型模式:适配器模式
  • 重大剧透:你不用ChatGPT,它砸你饭碗
  • 状态机模式
  • 瑞吉外卖:后台系统登录功能
  • Linux拓展:链接库
  • 基于.Net开发的、支持多平台、多语言餐厅点餐系统
  • Windows系统SSL/TLS安全协议介绍
  • ovs-vsctl 命令详解
  • 具备“记忆”功能的VBA目录选择器