-1

I'm having problems with my QToolbar.

I got a QToolBar and used a QVBoxLayout inside it to dispose my QPushButtons.

My problem was when I floated my ToolBar around the QVBoxLayout used to make lose its layout. After I put it back inside de dock the layout was back to normal.

I'd like to know if there is any way to fix the same layout for any situation.

I tried to implement two different layouts for each status, fixed and floating, but also didn't work.

The only way I found was forget the QToolBar and re-implementing my own personalized QWidget and simulate my own ToolBar so it did work well.

yurisnm
  • 1,630
  • 13
  • 29
  • Did you add a `QVBoxLayout` using `QToolBar::addWidget` method? (You shouldn't add such widgets to a tool bar) – frogatto Sep 06 '16 at 20:27
  • Can you provide an example so that people can give it a try? – Robert Sep 07 '16 at 03:30
  • That's basically what I've been doing: self.setFixedSize(102, 865) self.mywidget = QWidget() self.vbox = QVBoxLayout() self.vbox.setSpacing(0) self.vbox.setContentsMargins(10, 0, 0, 0) self.mywidget.setLayout(self.vbox) self.addWidget(self.mywidget) – yurisnm Sep 07 '16 at 11:41
  • http://i.imgur.com/4zGDZvY.png – yurisnm Sep 07 '16 at 11:52
  • @Y.Melo Please [edit] your question to include a [mcve], in the question itself not a screenshot. If you make it easier for people to help you, the more likely they are to bother. – Ajean Sep 09 '16 at 16:13
  • I just edited my question to be more specific. I also found the solution. The best solution was implement my own ToolBar. – yurisnm Sep 23 '16 at 14:42

2 Answers2

1

QToolBar takes care of its own layout, you shouldn't be worry about it. You should add actions or widgets and it automatically lays out the items horizontally or vertically regarding the orientation.

QToolBar Doc:

Toolbar buttons are added by adding actions, using addAction() or insertAction(). Groups of buttons can be separated using addSeparator() or insertSeparator(). If a toolbar button is not appropriate, a widget can be inserted instead using addWidget() or insertWidget(). Examples of suitable widgets are QSpinBox, QDoubleSpinBox, and QComboBox.

For example, the following code snippet yields the following layouts:

ui->mainToolBar->addWidget(new QPushButton("A"));
ui->mainToolBar->addWidget(new QPushButton("B"));
ui->mainToolBar->addWidget(new QPushButton("C"));
  • Horizontal:

    enter image description here

  • Vertical:

    enter image description here

  • Float:

    enter image description here

frogatto
  • 28,539
  • 11
  • 83
  • 129
0

You should not change the layout of the QToolBar. It manages its own layout, by doing so you have somewhat broken it.

QToolBar is a high level widget which handles its content with QActions. You need to create actions in your application, and add these actions using addAction() or insertAction().

Styling the QToolBar should be done either by subclassing your current QStyle(difficult) or using stylesheets(easy)

frogatto
  • 28,539
  • 11
  • 83
  • 129
galinette
  • 8,896
  • 2
  • 36
  • 87
  • 1
    So what u recommend me is to not use buttons inside a layout in my toolbar, is that it? I'd rather use "addActions" into my toolbar then play with it? I'm just starting with Qt. So, using Actions instead of QPushButtons in a QVBoxLayout inside my toolbar would still let me make changes like, "remove margins", "remove spacing", "set icons", and all these design changes? – yurisnm Sep 06 '16 at 20:22