赞
踩
通常情况下,对话框和消息框都有一组标准布局的按钮,QDialogButtonBox部件以当前部件的布局风格提供一系列标准按钮,并自动根据用户的桌面环境使用适当的布局,QDialogButtonBox允许开发人员向其添加按钮,这些按钮都遵循一定的角色(其角色定义见QDialogButtonBox.ButtonRole 取值表)。
QDialogButtonBox.StandardButton标准按钮:
QDialogButtonBox.ButtonRole 按钮角色:
ButtonRole与StandardButton标准按钮对应关系:
QDialogButtonBox常用方法:
QDialogButtonBox常用信号:
QDialogButtonBox类继承关系:
创建文件qdialogbuttonbox.py,演示添加两组标准按钮,和添加一组自定义按钮。完整代码如下:
- import sys
- from PyQt5 import QtCore, QtGui, QtWidgets
- from PyQt5.QtCore import Qt
- from PyQt5.QtWidgets import (QApplication, QWidget, QDialogButtonBox,
- QMessageBox, QPushButton, QVBoxLayout)
-
- class DemoDialogButtonBox(QWidget):
- def __init__(self, parent=None):
- super(DemoDialogButtonBox, self).__init__(parent)
-
- # 设置窗口标题
- self.setWindowTitle('实战PyQt5: QDialogButtonBox Demo!')
- # 设置窗口大小
- self.resize(400, 240)
-
- bbOkCancel = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel, self)
- bbOkCancel.button(QDialogButtonBox.Ok).setDefault(True)
- bbOkCancel.accepted.connect(self.onAccepted)
- bbOkCancel.rejected.connect(self.onRejected)
-
- bbARI = QDialogButtonBox(QDialogButtonBox.Abort|QDialogButtonBox.Retry|QDialogButtonBox.Ignore, self)
- bbARI.accepted.connect(self.onAccepted)
- bbARI.rejected.connect(self.onRejected)
-
- #添加按钮
- bbVert = QDialogButtonBox(Qt.Vertical, self)
- btnAdd = QPushButton('加法', self)
- btnSub = QPushButton('减法', self)
- btnMul = QPushButton('乘法', self)
- btnDiv = QPushButton('除法', self)
-
- bbVert.addButton(btnAdd, QDialogButtonBox.AcceptRole)
- bbVert.addButton(btnSub, QDialogButtonBox.AcceptRole)
- bbVert.addButton(btnMul, QDialogButtonBox.AcceptRole)
- bbVert.addButton(btnDiv, QDialogButtonBox.AcceptRole)
- bbVert.accepted.connect(self.onAccepted)
- bbVert.rejected.connect(self.onRejected)
-
- vLayout = QVBoxLayout(self)
- vLayout.setSpacing(16)
- vLayout.addStretch()
- vLayout.addWidget(bbOkCancel)
- vLayout.addWidget(bbARI)
- vLayout.addWidget(bbVert)
- vLayout.addStretch()
-
- self.setLayout(vLayout)
-
- def onAccepted(self):
- QMessageBox.information(self, '信息', 'accepted!!!')
-
- def onRejected(self):
- QMessageBox.information(self, '信息', 'rejected!!!')
-
-
-
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- window = DemoDialogButtonBox()
- window.show()
- sys.exit(app.exec())
运行结果如下图:
测试QDialogButtonBox
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。