PyQt:QDialogBu​​ttonBox有3个选项

时间:2018-02-18 22:38:31

标签: python pyqt

我正在尝试通过三个按钮对话框为用户创建警报,询问他们是否要保存更改,或者只是完全取消操作。我可以管理是和取消,但'不保存'选项不起作用。这就是我所拥有的:

import logging
import Qt
# from functools import wraps
from Qt import QtGui, QtCore, QtWidgets

dialog = QtWidgets.QDialog() #parent=parent)
dialog.setWindowTitle("Foo")
dialog.verticalLayout = QtWidgets.QVBoxLayout(dialog)

# Create the dialogs main message (Qlabel)
dialog.label = QtWidgets.QLabel(dialog)
dialog.label.setAlignment(QtCore.Qt.AlignCenter)
dialog.label.setTextInteractionFlags(dialog.label.textInteractionFlags() | QtCore.Qt.TextBrowserInteraction)
dialog.label.setTextFormat(QtCore.Qt.RichText)
dialog.label.setOpenExternalLinks(True)
dialog.label.setText("Magoo?")
dialog.verticalLayout.addWidget(dialog.label)

dialog.widget = QtWidgets.QWidget(dialog)
dialog.horizontalLayout = QtWidgets.QHBoxLayout(dialog.widget)
dialog.horizontalLayout.setContentsMargins(-1, -1, -1, 0)
dialog.horizontalLayout.setObjectName("horizontalLayout")
dialog.verticalLayout.addWidget(dialog.widget)

# Create the buttonbox with options
dialog.buttonBox = QtWidgets.QDialogButtonBox(dialog.widget)
dialog.buttonBox.setOrientation(QtCore.Qt.Horizontal)
dialog.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Discard | QtWidgets.QDialogButtonBox.Cancel | QtWidgets.QDialogButtonBox.Yes)
dialog.horizontalLayout.addWidget(dialog.buttonBox)

# Connect the buttonbox signals
dialog.buttonBox.accepted.connect(dialog.accept)
dialog.buttonBox.rejected.connect(dialog.reject)
QtCore.QMetaObject.connectSlotsByName(dialog)

# Resize the dialog box to scale to its contents
dialog.adjustSize()

# Launch the dialog
yes = dialog.exec_()
dont_notify_again = dialog.checkBox.isChecked()

我可能错过了与信号的连接应该是什么,但我不确定它们是什么。

1 个答案:

答案 0 :(得分:2)

为您的代码试用此解决方案。

result = QtGui.QMessageBox.question(self,
                                    "Message-Box title",
                                    "Message description",
                                    QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | QtGui.QMessageBox.Save)
    if result == QtGui.QMessageBox.Yes:
        print "Yes "
    elif result == QtGui.QMessageBox.No:
        print "No "
    elif result == QtGui.QMessageBox.Save:
        print "Save "