Unsolved Qt 5.5 add layout to widget without layout
-
I set all widgets and layouts using Qt Designer.
But I leave one QWidget (widgetOccContainer) without any layout. This widget has 2 items: tree view and horizontal layout (treeViewOccGroups & horizontalLayoutOccupations).
My idea is to add layout later to it. Here is how I did this:occupationsLayout = new QHBoxLayout(); occupationsLayout->addWidget(ui->treeViewOccGroups); occupationsLayout->addItem(ui->horizontalLayoutOccupations); ui->widgetOccContainer->setLayout(occupationsLayout);
The program crashes on last line.
If I add widget/item after setLayout(), then the program doesn't crash, but horizontalLayoutOccupations is not working: it is not arranging items properly.I tried creating:
occupationsLayout = new QHBoxLayout(ui->widgetOccContainer);
but it just behaves as if I use setLayout().
EDIT
occupationsLayout arranges only child widgets, but not child layouts (why?).In other words, if instead of adding horizontalLayoutOccupations I add a widget that has layout set to horizontalLayoutOccupations, this will work.
Why can't layout be a child to widget?
-
Hi @Borzh,
I don't know exactly what's wrong, but you can try a few things.
But I leave one QWidget (widgetOccContainer) without any layout. This widget has 2 items: tree view and horizontal layout (treeViewOccGroups & horizontalLayoutOccupations).
Since you want to add the layout later in C++, perhaps you can create
treeViewOccGroups
andhorizontalLayoutOccupations
in C++ too.My idea is to add layout later to it. Here is how I did this:
occupationsLayout->addItem(ui->horizontalLayoutOccupations);
I don't think you should call
addItem()
directly. TryaddLayout()
instead.If I add widget/item after setLayout(), then the program doesn't crash, but the layout is not working: it is not arranging items properly.
Do you mean
occupationsLayout
is not working, orhorizontalLayoutOccupations
is not working?ui->widgetOccContainer->setLayout(occupationsLayout);
The program crashes on last line.
Note: If you have a memory corruption bug in your code, then your program might crash at a line that is not related to your real problem.
-
Thanks for your answer. The occupationsLayout is really a custom layout, I just put QHBoxLayout as example, it doesn't work either. I mean occupationsLayout is not working, it is not resizing horizontalLayoutOccupations, although does resize treeViewOccGroups.
addLayout() doesn't help.
I don't think it is a memory corruption problem, because those are first lines of code in mainWindow constructor.
Note that treeViewOccGroups and horizontalLayoutOccupations already are children of widgetOccContainer, maybe qt somehow tries to reparent them?The idea is to set all needed values of widgets by Qt Designer... I believe if I create everything from scratch it will work, but I would like to understand why this program is not working.
-
You can try to add empty layout in Designer (add layout and remove items from it) and in code just use it
ui->widgetOccContainer->layout->addWidget(ui->treeViewOccGroups); ...
-
Ok I think I understood the issue at least.
When you add a layout as a child of widget, this layout is not resized when widget is resized (why?). But if instead of layout you add another widget that has that layout, it will work!
But I still leave the thread opened modifying the question.
-
@Borzh said:
The occupationsLayout is really a custom layout
Do you mean you subclassed QLayout to create
occupationsLayout
? Can you show us the code?Note that treeViewOccGroups and horizontalLayoutOccupations already are children of widgetOccContainer, maybe qt somehow tries to reparent them?
When an object is added to a layout, that object gets reparented to the layout's parent. However, since your layout is attached to
widgetOccContainer
, thentreeViewOccGroups
andhorizontalLayoutOccupations
should not change parents.You can investigate yourself like this:
// If the object has a non-empty objectName, then qDebug will print that name qDebug() << ui->treeViewOccGroups->parent();
-
@JKSH said:
Do you mean you subclassed QLayout to create
occupationsLayout
? Can you show us the code?It is just a QCardLayout in Qt examples (I use it to switch from one widget to another; in this case I switch from QTreeView "occupation groups" to layout that contains 2 QTreeViews: "occupation industry" and "occupation").
When an object is added to a layout, that object gets reparented to the layout's parent. However, since your layout is attached to
widgetOccContainer
, thentreeViewOccGroups
andhorizontalLayoutOccupations
should not change parents.Yes, I agree, they shouldn't, and debugging the program as you indicated, they don't.
Anyway, the problem was solved adding instead of layout -> widget that contains that layout. Strangely, but works. -
@Borzh said:
It is just a QCardLayout in Qt examples (I use it to switch from one widget to another; in this case I switch from QTreeView "occupation groups" to layout that contains 2 QTreeViews: "occupation industry" and "occupation").
You mean this one?: http://doc.qt.io/qt-5/layout.html#how-to-write-a-custom-layout-manager That implementation is incomplete (see "Further Notes"). Perhaps the card layout is missing the code required to manage a sub-layouts properly (I haven't investigated this in detail).
But anyway, I'm glad to hear that you have a working solution!
-
@JKSH said:
You mean this one?: http://doc.qt.io/qt-5/layout.html#how-to-write-a-custom-layout-manager
Correct, that one. But in the code I tried replacing it with QHBoxLayout without luck, layout wasn't managing sub-layout properly. I suppose QHBoxLayout has complete implementation.
But anyway, I'm glad to hear that you have a working solution!
Thanks for your help. Later I will fight a little more with layouts and if I find a solution, will post it here.