Nobilis
Nobilis

Reputation: 7458

PyQt4: Reorder Ok and Cancel buttons in the QDialogButtonBox

How can I override the standard order of buttons in the QDialogButtonBox class?

It seems that PyQt follows some kind of standard based on the platform it runs on.

At the moment Cancel is on the left and Ok on the right (I'm on CentOS 6):

enter image description here

but my users find it confusing and counter to what they're used to and have asked me to swap them:

enter image description here

This is how I create the box:

    self.buttonBox = QtGui.QDialogButtonBox(self)
    self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
    self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Ok|QtGui.QDialogButtonBox.Cancel)

Upvotes: 3

Views: 4729

Answers (1)

NoDataDumpNoContribution
NoDataDumpNoContribution

Reputation: 10864

I thought about changing the direction in the layout containing the buttons:

buttonBox.layout().setDirection(QtGui.QBoxLayout.RightToLeft)

In the following example:

from PySide import QtGui, QtCore
app = QtGui.QApplication([])

buttonBox = QtGui.QDialogButtonBox()
buttonBox.setOrientation(QtCore.Qt.Horizontal)
buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Ok|QtGui.QDialogButtonBox.Cancel)
buttonBox.layout().setDirection(QtGui.QBoxLayout.RightToLeft) # with this line the order of the buttons swap

buttonBox.show()
app.exec_()

it reverses the order for me (If RightToLeft doesn't work, also try QtGui.QBoxLayout.LeftToRight).

Upvotes: 4

Related Questions