简体   繁体   中英

PyQT center toolbar buttons

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

I am not sure I understand correctly, but if you are looking for a way to center buttons on toolbar with respect to QMainWindow , then yes there is a (hackish) way. You just need to put a widget that acts like a 'spacer'. That is basically a QWidget with expanding size policy.

Here is a minimal example:

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

Which gives you this:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM