[PyQt] Toolbar widget sizing problems

David Boddie david at boddie.org.uk
Sat Sep 6 15:48:42 BST 2008


On Saturday 06 September 2008, Scott Price wrote:

> Well, what you say makes sense and I follow your reasoning, however it
> still doesn't seem to work for making the line-edit show the size I want it
> to.
>
> Giving the group-box an explicit parent doesn't work, that is.

OK, perhaps I'm seeing very different behaviour (with PyQt 4.3 and Qt 4.3.2
on Linux). I found that the group box and its line edit didn't appear at all.

> The only thing I could find in the documentation regarding
> QToolBar.addWidget() is the reference to not being able to populate the
> extension pop up with widgets added using addWidget(), but this only seems
> to apply when the toolbar is not owned by the main window.  It also doesn't
> speak to the toolbar not taking ownership of the widget that is added, nor
> to disregarding the widgets' geometry.

Yes, this might not be relevant to your problem.

> It's not a great problem, as it _does_ show the widget, just not the size I
> tell it to use... Oh well, there are worse things in life I suppose.

Bear in mind that the group box is imposing its own constraints on the size
of everything, and that it will also take up a lot of space on the toolbar.
Have you tried inserting a line edit on its own into the toolbar for testing
purposes?

> Just doing some testing, changing the QLineEdit to a QTextEdit gives some
> strange results:  the resulting text box shows on the toolbar as a square
> of about 150x150 pixels...  not exactly an optimum substitution, even
> though it is larger than the line-edit :-).

QTextEdit has different size constraints to QLineEdit in that it will expand
vertically - the Expanding size policy. QGroupBox uses its default width and
height in preference to anything else - the Preferred size policy.

This typically means that a line edit inside a group box will take up all the
space offered to it by the group box, but won't cause the group box to
expand.

> Toolbars seem to be optimized for showing only toolbuttons, and even those
> I found it doesn't like to resize either; I notice the documentation does
> speak of the extension pop up being fixed in a future edition, perhaps the
> resizing issue will be fixed as well?

You can set the default icon size with QToolBar.setIconSize():

http://www.riverbankcomputing.com/static/Docs/PyQt4/html/qtoolbar.html#setIconSize

> If I can't make it work I'll just discard the toolbar and replace it with a
> layout on the main window.

Or you could use a dock widget instead. It may be more suitable than a
toolbar for showing large widgets.

David


More information about the PyQt mailing list