gpt4 book ai didi

qt - 不同的 QMessageBox.Roles 是什么意思?

转载 作者:行者123 更新时间:2023-12-04 13:11:05 24 4
gpt4 key购买 nike

我正在探索自定义警告消息框的行为,我使用以下方法向其中添加了不同的按钮:

msgBox.addButton(<button text>, QtGui.QMessageBox.XRole)

其中不同的可能角色(即 X 的值)是 enumerated at the documentation .

我的问题是,这些角色的状态究竟是什么,它们的使用规则是什么?我在文档中找不到任何明确的内容,并且当我使用除 Destructive 之外的任何角色时, Accept , 或 Reject , 当 QMessageBox执行它似乎没有正确返回角色,如下面的简单示例所示:
# -*- coding: utf-8 -*-
import sys
from PySide import QtGui

class Form(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
#Make a few buttons
warnButton=QtGui.QPushButton("Warn Me")
self.readoutLabel=QtGui.QLabel("Waiting for inputs from dialog")
layout = QtGui.QVBoxLayout()
layout.addWidget(warnButton)
layout.addWidget(self.readoutLabel)
self.setLayout(layout)
warnButton.clicked.connect(self.warnSlot)

def warnSlot(self):
self.readoutLabel.setText("Waiting for inputs from dialog")
msgBox = QtGui.QMessageBox(QtGui.QMessageBox.Warning,
"QMessageBox(QMessageBox.Warning...)", "Will you heed this fancy custom warning?!",
QtGui.QMessageBox.NoButton, self)
msgBox.addButton("Accept", QtGui.QMessageBox.AcceptRole)
msgBox.addButton("I reject you!", QtGui.QMessageBox.RejectRole)
msgBox.addButton("Destroy!", QtGui.QMessageBox.DestructiveRole)
msgBox.addButton("Yes!", QtGui.QMessageBox.YesRole)
reply=msgBox.exec_() #seems to always be 0,1, or 2
print "Actual reply: " , reply
if reply == QtGui.QMessageBox.DestructiveRole:
self.readoutLabel.setText("Response to warning: Destroy")
elif reply == QtGui.QMessageBox.RejectRole:
self.readoutLabel.setText("Response to warning: Reject")
elif reply == QtGui.QMessageBox.AcceptRole:
self.readoutLabel.setText("Response to warning: Accept")
elif reply == QtGui.QMessageBox.YesRole:
self.readoutLabel.setText("Response to warning: Yes")

if __name__ == '__main__':
qtApp = QtGui.QApplication(sys.argv)
ex = Form()
ex.show()
sys.exit(qtApp.exec_())

当您单击"is"按钮时,与其他按钮不同,它似乎没有被调用者注册。这些角色似乎不是惰性的,但具有我一无所知的潜在机制和使用期望。

最佳答案

QMessageBox::exec的返回值只有在对话框中使用标准按钮时才有意义。但是您在技术上使用自定义按钮,在这种情况下 exec()返回按钮索引(但你不应该依赖这个事实,因为它没有在文档中修复)。除"is"按钮之外的其他按钮的正确结果仅仅是巧合。如果您在添加“拒绝”按钮之后添加“接受”按钮,您也会开始收到这些按钮的错误结果。

您可以使用 QMessageBox::buttonRoleQMessageBox::currentButton获得实际角色:

msgBox.exec_()
reply = msgBox.buttonRole(msgBox.clickedButton())

如果您有多个具有相同作用的按钮,您可以保存每个按钮对象并进行比较 QMessageBox::currentButton到每个按钮对象:
yes_button = msgBox.addButton("Yes!", QtGui.QMessageBox.YesRole)
msgBox.exec_()
if msgBox.clickedButton() == yes_button:
print("Response to warning: Yes")

按钮角色的目的是为消息框提供与平台一致的外观。按钮的位置将根据其角色和当前操作系统而有所不同。例如,“帮助”按钮将出现在左侧或右侧,具体取决于它通常出现在当前操作系统的 native 对话框中的位置。

注: exec()如果您使用标准按钮,将给出正确的结果。奇怪的是我没有找到在 PySide 中使用它们的方法: QMessageBox::addButton(StandardButton)在 PySide 中不可用; QMessageBox::setStandardButtons似乎没有任何效果;使用构造函数的“按钮”参数工作不正确。也许我做错了什么。

关于qt - 不同的 QMessageBox.Roles 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25101171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com