Take the 2-minute tour ×

By default the toolbar buttons in PyQT are aligned to the left, is it possible to make them centered so that they slide along when resizing?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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
# some dummy actions
# and the right one


Which gives you this:

enter image description here

share|improve this answer
Awesomeballs, thanks! –  FLX Dec 17 '11 at 16:42
add comment

Your Answer


