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

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

本文目录

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

PyQt5桌面应用系列

  • PyQt5桌面应用开发(1):需求分析
  • PyQt5桌面应用开发(2):事件循环
  • PyQt5桌面应用开发(3):并行设计
  • PyQt5桌面应用开发(4):界面设计
  • PyQt5桌面应用开发(5):对话框
  • PyQt5桌面应用开发(7):文本编辑+语法高亮与行号

对话框

对话框在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 sys

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QHBoxLayout, QPushButton, QWidget, QVBoxLayout, \
    QTextEdit, QDialogButtonBox, QLabel, QMessageBox


class 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 dialog
        self.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 bs


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = QMainWindow()
    win.setWindowTitle("Dialog buttons")

    # center widget
    center = 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 window
    win.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.kler.cn/news/17083.html

相关文章:

  • Java 基础进阶篇(二)—— static 静态关键字与单例模式
  • kafka 学习,笔记
  • Spring Boot参考指南-Spring Boot安装(Maven安装、Gradle安装)
  • Docker compose 常用指令
  • c++ 11标准模板(STL) std::vector (二)
  • 天气预报查询 API 提供个性化的天气服务的设计思路
  • 贪心刷题~
  • AI 时代,提示词便是生产力
  • ChatGPT AI使用成本
  • 【每日随笔】操控人性 ③ ( 懂领导的心思 | 办事的套路 | 管理学与权谋 | 人事谱系 )
  • HDU5552 Bus Routes(分治NTT)
  • 每天一道算法练习题--Day16 第一章 --算法专题 --- ----------哈夫曼编码和游程编码
  • SpringCloud:ElasticSearch之数据同步
  • 【实例展示通俗易懂】SQL中的内外连接、左右连接
  • Vue3+Element Plus环境搭建和一键切换明暗主题的配置
  • 【Latex】有关于Latex tabularray的一些很不错的教程、模板
  • LeetCode周赛复盘(第343场周赛)
  • isNotBlank 和isNotEmpty的区别
  • 网络安全 等级保护 网络设备、安全设备知识点汇总
  • Nachos系统的上下文切换
  • Latex 定理和证明类环境(amsthm)和(ntheorm)的区别
  • 每日一题142——最少操作使数组递增
  • 【Linux超强学习路线图】赶紧收藏学习!
  • 数据库管理-第七十二期 复盘(20230505)
  • 【TCP为什么需要粘包和拆包】
  • LeetCode_双指针_中等_24.两两交换链表中的节点
  • 使用dataFEED OPC Suite将西门子PLC数据转发至REST API
  • FL Studio21没有language选项?如何设置切换中文语言
  • 《论文阅读》开放域对话摘要(长文本|知识嵌入)
  • 《花雕学AI》31:ChatGPT--用关键词/咒语/提示词Prompt激发AI绘画的无限创意!