Results 1 to 4 of 4

Thread: Adding stretch to QToolBar in Qt4.3

  1. #1
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Red face Adding stretch to QToolBar in Qt4.3

    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?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding stretch to QToolBar in Qt4.3

    What about adding a widget( that contains nothing) via addWidget(), and setting the fixed size of this widget to whatever you need?

    Regards

  3. The following user says thank you to marcel for this useful post:

    THRESHE (7th February 2008)

  4. #3
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Adding stretch to QToolBar in Qt4.3

    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...

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Adding stretch to QToolBar in Qt4.3

    Set the size policy of "stretch widget" to QSizePolicy::Expanding.
    J-P Nurmi

  6. The following 2 users say thank you to jpn for this useful post:

    drhex (4th June 2007), SubV (20th November 2007)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.