How to add stretchable spacer in Qtoolbar?

16,807

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);
Share:
16,807

Related videos on Youtube

steffen
Author by

steffen

I am a Python freak and data nerd. Also, I am experimenting with creating videos on youtube. About Python and data. ¯(°_o)/¯ https://www.youtube.com/channel/UCG9XNnq9LodijOBpIVy1ILg

Updated on June 15, 2022

Comments

  • steffen
    steffen about 2 years

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

    I use Qt Creator but I am not afraid of editing source, so either solution is greatly appreciated.

  • steffen
    steffen over 11 years
    I used it right after ui->setupUi with insertWidget() instead of addWidget() in order to place the space where I want it to be.
  • Exa
    Exa over 8 years
    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);