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

eri*_*ric 3 qt pyqt pyside

我正在探索自定义警告消息框的行为,我使用以下命令添加不同的按钮:

msgBox.addButton(<button text>, QtGui.QMessageBox.XRole)
Run Code Online (Sandbox Code Playgroud)

文档列举了不同的可能角色(即X的值).

我的问题是,这些角色的状态究竟是什么,以及它们的使用规则是什么?我无法找到任何明确的文档中,当我用比其他任何角色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_())
Run Code Online (Sandbox Code Playgroud)

当您单击"是"按钮而不是其他按钮时,调用者似乎没有注册它.看起来这些角色并不是惰性的,而是具有我盲目的潜在机制和使用期望.

Pav*_*hov 5

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

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

msgBox.exec_()
reply = msgBox.buttonRole(msgBox.clickedButton())
Run Code Online (Sandbox Code Playgroud)

如果您有多个具有相同角色的按钮,则可以保存每个按钮对象并与QMessageBox::currentButton每个按钮对象进行比较:

yes_button = msgBox.addButton("Yes!", QtGui.QMessageBox.YesRole)
msgBox.exec_()
if msgBox.clickedButton() == yes_button:
  print("Response to warning: Yes")    
Run Code Online (Sandbox Code Playgroud)

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

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