【问题标题】:PyQt4: Reorder Ok and Cancel buttons in the QDialogButtonBoxPyQt4:重新排序 QDialogBu​​ttonBox 中的确定和取消按钮
【发布时间】:2014-08-04 05:47:16
【问题描述】:

如何覆盖 QDialogBu​​ttonBox 类中按钮的标准顺序?

看来 PyQt follows some kind of standard based on the platform it runs on.

目前左边是Cancel,右边是Ok(我在CentOS 6上):

但我的用户发现它令人困惑并且与他们习惯的背道而驰,并要求我更换它们:

这就是我创建盒子的方式:

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

【问题讨论】:

  • 您尝试过使用QDialogButtonBox::AcceptRoleQDialogButtonBox::RejectRole 吗?
  • @Thomas 我使用buttonBox.acceptedbuttonBox.rejected 链接到我想在按下确定或取消时调用的相应方法。这是你的意思吗?
  • 不完全是,如果您尝试使用 `addButton()` 而不是 setStdBtn 会发生什么?
  • 我没有,但我想知道是否可以避免这样做?据我了解,使用setStandardButtons 设置带有信号的按钮,我可以直接链接到这些信号(使用acceptedrejected),而不必将它们自定义为我需要的取消和确定按钮如果我使用了addButton
  • 如果你不需要标准排序,你也不需要 QDialogBu​​ttonBox。

标签: python qt user-interface pyqt4


【解决方案1】:

我想过改变包含按钮的布局中的方向:

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

它会颠倒我的顺序(如果 RightToLeft 不起作用,也试试QtGui.QBoxLayout.LeftToRight)。

【讨论】:

  • 但这会改变更多的东西,而不仅仅是按钮?
  • @FinalContest QDialogBu​​ttonBox 是否用于按钮以外的其他东西?
  • 啊,我明白你的意思了。这不一定适用于 >=3 按钮,但可能适用于两个按钮,是的。最理想的解决方案是“定义顺序”。
  • @FinalContest 然后我会按照 Thomas 在对该问题的评论中的建议并使用 addButton() 并按照我喜欢的顺序手动添加按钮。
猜你喜欢
  • 2015-01-20
  • 2015-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
相关资源
最近更新 更多