Left align QToolButtons in a vertical QToolBar



  • I've googled a lot this question and did not find a real answer. By default those buttons are centered and there's no simple way (stylesheet, property) to change that.
    I'm ready to subclass QStyle if that's the way to go but I'm still clueless on where in the code is defined the size and position of a QToolButton

    Basically here is what I want to achieve:
    hey


  • Moderators

    If you're not gonna change the items much you can "fix" them like this:

    QLayout* lay = toolBar->layout();
    for(int i = 0; i < lay->count(); ++i)
        lay->itemAt(i)->setAlignment(Qt::AlignLeft);
    

    otherwise you'll need to carefully fix any new item added.

    Btw. And why on earth are you developing on a 15 years old OS :P



  • Thanks for your answer.
    That's an interesting option, however it is still quite ugly because the buttons all have a different width (instead of using 100% of the toobar width)


  • Moderators

    You can set a fixed size buttons for example with stylesheet:

    QToolBar QToolButton { width: 100px;  } //or switch that to min-width if you prefer
    

    EDIT: Scratch that. You would loose left alignment again this way.



  • Setting minimum size on toolbutton should work.
    I do not see why Chris says it would not,

    I believe icon is (or at least was last time checked) painted aligned to the left margin and text is painted right next to it.
    So specifying minimum size should do the job.


  • Moderators

    @alex_malyu Icon and text is centered inside tool button, not aligned left (at least on Windows, don't know about other platforms).
    If you set a width then the buttons are placed properly, but the problem shifts from the toolbar to the tool button. Since there doesn't seem to be a way to set alignment of tool button content (or is there?) and you can't provide a custom widget for the toolbar without modifying the code "outside", the only other, non-intrusive to the user code solution I can think of is to set the width as I posted and implement your own style that would paint QStyle::CE_ToolButtonLabel aligned left (as suggested in that other thread).



  • It looks like I've done it in Qt4 for QPushButton
    It did have 'text-align' property you could specify in the Stylesheet.

    I could not find it looking in Qt 5 documentation.
    It seems instead of adding it as common for AbstractButton to all buttons property it was removed.
    Kind of disappointing.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.