easythrees
easythrees

Reputation: 1650

PyQt: QDialogButtonBox with 3 options

I'm trying to create an alert for a user via a three button dialog box asking if they'd like to save their changes or not, or just cancel out of the operation altogether. I can manage the yes and the canceling out, but the 'don't save' option isn't working. Here is what I have:

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()

I am probably missing what the connection to the signals should be, but I'm not sure what those are.

Upvotes: 2

Views: 2341

Answers (1)

Binit Amin
Binit Amin

Reputation: 501

Try this solution for your code.

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 "

Upvotes: 5

Related Questions