How to add a stretchable spacer in a QToolbar?
Asked Answered
B

1

24

I want some of my toolbar actions to appear left-bound and some right-bound. In GTK, I remember adding a stretchable (expandable) separator. How do I achieve that in Qt?

Biggs answered 4/10, 2012 at 15:23 Comment(2)
Duplicate of #2062478Saltish
Does this answer your question? How do I align a QWidget to the right of a QToolBar?Lucchesi
A
49

You can use an empty widget with automatic expanding, it works like the spacers you can use in Qt Designer:

tb = my_toolbar;

QWidget* empty = new QWidget();
empty->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
tb->addWidget(empty);

tb->addWidget(otherWidget);
Amphibiotic answered 4/10, 2012 at 16:3 Comment(2)
I used it right after ui->setupUi with insertWidget() instead of addWidget() in order to place the space where I want it to be.Biggs
Note that this only works for toolbars which are attached on the top or bottom of your window. For toolbars that are attachable to the left or right you also need to set the vertical size policy to Expanding: empty->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);Farly

© 2022 - 2024 — McMap. All rights reserved.