1

I use the Qt Designer to design .ui files and compile them in my VS-Project. We have a Toolbar class, derived from QToolbar.

Class initialization looks like this.

MyToolBar::ToolBar(QWidget* qWidgetParent) :
    ToolBar(qWidgetParent),
    mUiToolBar(new Ui::MyToolBar())
    {
        mUiToolBar->setupUi(this);
        //...
     }

Everything works fine for fixed positions and sizes. But if I start using a layout for automatic formating everything is broken. In this case everything is on the topleft position.

Any Idea whats happen in this case? What is the correct way to use layouts in a QToolbar?

edit: i have copied the code from my project. In this MyToolBar is derived by ToolBar, a class for managing the same functionality. ToolBar is derived by QToolBar.

norca
  • 138
  • 1
  • 14
  • Can you show how you are adding widgets to the `MyToolBar`? I assume `MyToolbar` is inherited from `QToolBar`, not from `Toolbar`? – Nemanja Boric Feb 04 '13 at 11:48
  • This was a mistake from copy code. MyToolBar is dervied by ToolBar by QToolBar. I edit my post. – norca Feb 04 '13 at 13:05

2 Answers2

1

I don't think a ToolBar is meant to have a Layout. What are you trying to achieve with that?

Seeing everything in the topleft position that should be in a Layout is a sign that either those child Widgets were not added to the Layout (using QLayout::addWidget(...)) or that the Layout was not added to the parent Widget (your ToolBar, using QWidget::setLayout(...)).

Also, you probably meant to call QToolBar(qWidgetParent), in stead of ToolBar(qWidgetParent),.

PrisonMonkeys
  • 1,199
  • 1
  • 10
  • 20
  • I think the best is to remove the layout. I am not able to find a good Widget <-> Layout dependency. – norca Feb 04 '13 at 15:27
  • @norca Then add the Widgets to your ToolBar using the QToolBar::addWidget(...) function. But normally a QToolBar should only have QActions, which you can add with QToolBar::addAction(...). – PrisonMonkeys Feb 04 '13 at 15:40
  • My toolbar has buttons and checkboxes, we aimed to have everything scaleable with layout. – norca Feb 05 '13 at 12:52
0

You are forgetting to call QToolBar::addWidget(), but you are setting parent of the widgets to the QToolBar widget.

This will create all widgets at (0,0) position (relative to the parent), but it will not layout them as you are expecting to.

Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91