dvilela
dvilela

Reputation: 1262

Check which button in an QMessageBox was clicked

How should I check which one of the buttons in the following QMessageBox was clicked? The button == QMessageBox.Ok is not working.

class WarningWindow(QMessageBox):

    nextWindowSignal = pyqtSignal()

    def __init__(self):
        super().__init__()

        self.setWindowTitle('A title')
        self.setText("Generic text")
        self.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
        self.buttonClicked.connect(self.nextWindow)

    def nextWindow(self, button):
        if button == QMessageBox.Ok:
            print('ok')
        elif button == QMessageBox.Cancel:
            print('cancel')
        else:
            print('other)

        self.nextWindowSignal.emit()

Upvotes: 4

Views: 1433

Answers (1)

eyllanesc
eyllanesc

Reputation: 243887

You have to convert the QAbstractButton to a QMessageBox::StandardButton and then do the comparison:

def nextWindow(self, button):
    sb = self.standardButton(button)
    if sb == QMessageBox.Ok:
        print("ok")
    elif sb == QMessageBox.Cancel:
        print("cancel")
    else:
        print("other")
    self.nextWindowSignal.emit()

Upvotes: 4

Related Questions