python - PyQT 中心工具栏按钮

标签 python pyqt pyqt4

默认情况下,PyQT 中的工具栏按钮是左对齐的,是否可以使它们居中以便在调整大小时滑动?

最佳答案

我不确定我是否理解正确,但是如果您正在寻找一种方法使工具栏上的按钮相对于QMainWindow居中,那么是的,有一个(hackish ) 方式。您只需要放置一个像“垫片”一样的小部件。这基本上是一个具有扩展大小策略的QWidget

这是一个最小的例子:

import sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)
main = QtGui.QMainWindow()
toolbar = QtGui.QToolBar()

# spacer widget for left
left_spacer = QtGui.QWidget()
left_spacer.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
# spacer widget for right
# you can't add the same widget to both left and right. you need two different widgets.
right_spacer = QtGui.QWidget()
right_spacer.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)

# here goes the left one
toolbar.addWidget(left_spacer)
# some dummy actions
toolbar.addAction('one')
toolbar.addAction('two')
toolbar.addAction('three')
# and the right one
toolbar.addWidget(right_spacer)

main.addToolBar(toolbar)
main.show()
sys.exit(app.exec_())

这给了你这个:

enter image description here

关于python - PyQT 中心工具栏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8537474/

相关文章:

python - 按住鼠标按钮 10 秒后连接信号 PyQt Python

python - PyQt:初始化重置的默认值

css - 为 QSpinBox/QComboBox 设置边框半径会对上/下控件产生意外的副作用

python - 是否需要removeWidget()

python - 扭曲:无用的 "AlreadyCalled"错误

python - 调整闹钟时间以匹配python中的时钟时间

python - 你如何从没有结尾的管道中读取python中的标准输入

python - pandas 合并两个数据框

python - Qt中实现一维拖动操作

python - PyQt:防止在 QDialog 中调整大小和最大化?