PyQt4: 在QDialogButtonBox中重新排序确认和取消按钮

3 投票
1 回答
4436 浏览
提问于 2025-04-18 09:42

我怎么才能改变QDialogButtonBox类中按钮的默认顺序呢?

看起来PyQt会根据运行的平台遵循某种标准。

目前在我的系统上,取消按钮在左边,确定按钮在右边(我使用的是CentOS 6):

这里插入图片描述

但是我的用户觉得这样很困惑,不符合他们的习惯,他们希望我把这两个按钮的位置调换一下:

这里插入图片描述

这是我创建这个按钮框的代码:

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

1 个回答

4

我考虑过要改变包含按钮的布局方向:

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

在下面这个例子中:

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_()

它会帮我把顺序反过来(如果右到左的方向不管用,可以试试 QtGui.QBoxLayout.LeftToRight)。

撰写回答