Search code examples
pythonpyqt5qmessagebox

How to order PyQt Qmessagebox Buttons


I have 4 custom buttons in my Qmessagebox, and I want them to be in order: (None, Small, Medium, Large).

msgbox.setStandardButtons(QMessageBox.Yes | QMessageBox.No | QMessageBox.Ignore | QMessageBox.Retry)
msgbox.setDefaultButton(QMessageBox.Yes)
buttonX = msgbox.button(QMessageBox.Yes)
buttonY = msgbox.button(QMessageBox.No)
buttonZ = msgbox.button(QMessageBox.Ignore)
buttonA = msgbox.button(QMessageBox.Retry)
buttonX.setText("None")
buttonY.setText("Small")
buttonZ.setText("Medium")
buttonA.setText("Large")

result = msgbox.exec_()

Currently, the default button is Qmessagebox.Yes button ("None"). How can I change the order of the buttons without switching around the name of each button?

EDIT: I use python 3.8 and PyQt5


Solution

  • The order of the buttons is predefined by the roles so a possible solution is to remove the "Medium" button and add it at the end:

    # ...
    buttonA.setText("Large")
    
    hlay = msgbox.findChild(QHBoxLayout)
    litem = hlay.takeAt(3)
    w = litem.widget()
    if w is not None:
        hlay.addWidget(w)
    
    result = msgbox.exec_()