yem
yem

Reputation: 755

Captures the close button as QMessageBox.Ok

I have an if statement in my QMessageBox that is supposed to check whether the user clicked on okay, but when the user closes the QMessageBox the statement is true for some reason (despite the user not clicking on Ok).

def update_msgbox(self):
        from PyQt5.QtWidgets import QLabel, QDialogButtonBox
        self.msg = QMessageBox()
        self.grid_layout = self.msg.layout()

        self.qt_msgboxex_icon_label = self.msg.findChild(QLabel, "qt_msgboxex_icon_label")
        self.qt_msgboxex_icon_label.deleteLater()

        self.qt_msgbox_label = self.msg.findChild(QLabel, "qt_msgbox_label")
        self.qt_msgbox_label.setAlignment(Qt.AlignCenter)
        self.grid_layout.removeWidget(self.qt_msgbox_label)

        self.qt_msgbox_buttonbox = self.msg.findChild(QDialogButtonBox, "qt_msgbox_buttonbox")
        self.grid_layout.removeWidget(self.qt_msgbox_buttonbox)

        self.grid_layout.addWidget(self.qt_msgbox_label, 0, 0, alignment=Qt.AlignCenter)
        self.grid_layout.addWidget(self.qt_msgbox_buttonbox, 1, 0, alignment=Qt.AlignCenter)


        self.msg.setWindowTitle("  Software Update")
        self.msg.setText("A software update is available.<br>Do you want to update now?<br>")
        self.msg.setStandardButtons(QMessageBox.Ok)
        self.msg.setStyleSheet("QLabel{min-width: 200px;}")
        self.msg.setWindowIcon(QtGui.QIcon("CalculatorLogo(150p)_1.0.0.ico"))

        if self.msg.exec_() == QMessageBox.Ok:
                return True

        else: 
                return False

Upvotes: 0

Views: 923

Answers (1)

musicamante
musicamante

Reputation: 48231

Note: doing different things depending on the fact that the user has cancelled the message box instead of clicking the only button is not a very good idea, and will certainly result in crating a lot of confusion to the user. Consider adding a Cancel button instead.

A possible solution could be to add a cancel button anyway, but then hide it.

self.msg.setStandardButtons(QMessageBox.Ok|QMessageBox.Cancel)
self.msg.button(QMessageBox.Cancel).setVisible(False)

Upvotes: 1

Related Questions