一千萬個為什麽

搜索

PyQT中心工具欄按鈕

默認情況下,PyQT中的工具欄按鈕與左側對齊,是否可以使它們居中以便在調整大小時滑動?

最佳答案

我不確定我是否理解正確,但如果您正在尋找一種方法來中心工具欄上的按鈕 QMainWindow ,那麽肯定是(hackish)方式。你只需要放置一個像'spacer'的小部件。這基本上是帶有擴展大小策略的 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

轉載註明原文: PyQT中心工具欄按鈕