How to check if user pressed the cross in top right corner in QMessageBox?

35 Views Asked by At
            msgBox = QMessageBox()
            msgBox.setIcon(QMessageBox.Question) 
            # msgBox.setWindowIcon(QtGui.QIcon("whatsapp-logo.png"))
            msgBox.setText("Do you want to add more items?")  # set text   
            msgBox.setWindowTitle("Add more items")  
            msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No) 
            path="Purchasing_Order_"+str(pdf_vendorName)+"_"+str(pdf_contact)+'.pdf'

            returnValue = msgBox.exec()
            if returnValue == QMessageBox.Yes:
                    do this
            
            elif returnValue == QMessageBox.No:
                    generate_purchase_reciept(pdf_dataframe,multi_item_df)
                    do this
                    try:
                            msgBox = QMessageBox()
                            msgBox.setIcon(QMessageBox.Question) 
                            msgBox.setWindowIcon(QtGui.QIcon("whatsapp-logo.png"))
                            msgBox.setText("Do you want to send this bill to Client's WhatsApp?")  # set text   
                            msgBox.setWindowTitle("WhatsApp Message Send Option")  
                            msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
                            returnValue = msgBox.exec()
                            path="Purchasing_Order_"+str(pdf_vendorName)+"_"+str(pdf_contact)+'.pdf'
                            if returnValue == QMessageBox.Ok:
                                  do this

                    except:
                           do this

                    from MainPage import Ui_Form2
                    Form.close()
                    from MainPage import Ui_Form2 
                    self.MainWindow = QtWidgets.QMainWindow()
                    self.mainpage= Ui_Form2() # close after no
                    self.mainpage.setupUi(self.MainWindow)
                    self.MainWindow.show()

This is the code. I want to check whether user pressed the cross in the top right corner.

            elif returnValue == QMessageBox.Rejected:
# Handling when the message box is closed without making a choice or the cross button is 
                    print("Message box closed without making a choice")  

This is the addition I made. However, this block is never checked even when cross is clicked. The No block always runs.

1

There are 1 best solutions below

0
mahkitah On

Instead of doing returnValue = msgBox.exec() you can do msgBox.open() and connect to the msgBox.finished signal.
That will give you the int value of the button that was clicked (ButtonRole)
Yes: 16384
No: 65536
and closing the window NoButton = 0