24

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
  • 8,572
  • 11
  • 52
  • 90
  • Duplicate of https://stackoverflow.com/questions/2061978/how-do-i-align-qtwidget-to-right-in-the-qttoolbar – Rabter Jun 07 '23 at 07:09

1 Answers1

48

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);
Synxis
  • 9,236
  • 2
  • 42
  • 64
  • 3
    I used it right after ui->setupUi with insertWidget() instead of addWidget() in order to place the space where I want it to be. – steffen Oct 04 '12 at 16:17
  • 5
    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);` – Exa Oct 24 '15 at 23:28