PDA

View Full Version : Adding stretch to QToolBar in Qt4.3



drhex
2nd June 2007, 20:45
My main window has a QToolBar with icons for various tools.
If I just addAction()s to the toolbar they show up left adjusted with an equal amount of space between each, which is usually fine.

However, at some places in the toolbar, I need extra space between the icons, and the last icon is supposed to be right-adjusted in the toolbar. In ordinary widgets, those problems could be easily solved by calling addSpacing() and addStrech() on the layout.

I had a (somewhat ugly but) working solution in Qt4.2.3 - for spacing I added a 1x1 pixel transparent icon and for the stretch necessary to right-adjust the last icon I casted the Toolbar's layout() to QBoxLayout, which allowed me to call addStretch().

That last trick won't work anymore in Qt-4.3, QToolBar's layout is a "QToolBarLayout" (not in the docs) and its addItem() (called by addStretch() dies saying I should have called addAction() instead. That hint won't help me since QToolBar::addAction() does not have any spacing or stretch arguments.

Is there a proper way to add stretch to a QToolBar?

marcel
2nd June 2007, 20:50
What about adding a widget( that contains nothing) via addWidget(), and setting the fixed size of this widget to whatever you need?

Regards

drhex
2nd June 2007, 22:42
I guess that would work fine for the spacing, but for stretch?
Constantly changing the "fixed" size as the main widget is being resized by the user seems odd for a "proper" method...

jpn
3rd June 2007, 07:43
Set the size policy of "stretch widget" to QSizePolicy::Expanding (http://doc.trolltech.com/4.3/qsizepolicy.html#Policy-enum).