Problem with splitters and layouts
-
I have a dialog that was working. I have a QGridLayout called overallLayout. This layout contains a QVBoxLayout called leftLayout. leftLayout contains three QGridLayouts called topLayout, middleLayout, and bottomLayout. overallLayout is tied to the leftLayout with this line of code
overallLayout->addLayout(leftLayout, 0, 0, -1, 0);
That all works,
I want to add a splitter so that everything in the left layout is to the left and there is more stuff on the right, with the splitter between the two.
I create a rightWidget. I put a rightLayout within the rightWidget and then a button in that rightLayout. I then add a splitter to the overallLayout and add leftWidget and rightWidget to the splitter.
Two things happen. 1. When I display the dialog, it is displayed too small. I can manually expand it and it has all the widgets, but the initial display is not big enough to show most of the left side and bottom of the stuff to the left of the splitter, and the right side is not displayed at all. Until I manually expand it.
- The splitter doesn't work. When I try to drag it left or right, the display vibrates until I slide it far enough and the right or left side disappears.
I can't figure out what is wrong.
The new code looks like this
m_splitter = new QSplitter(this); m_leftWidget = new QWidget(m_splitter); m_leftWidget->setLayout(leftLayout); m_rightWidget = new QWidget(m_splitter); auto rightLayout = new QGridLayout; m_rightWidget->setLayout(rightLayout); auto button = new QPushButton("11111111111111111111111111111111"); rightLayout->addWidget(button, 0, 0, -1, 0); m_splitter->addWidget(m_leftWidget); m_splitter->addWidget(m_rightWidget); m_splitter->setCollapsible(0, true); m_splitter->setCollapsible(1, true); m_splitter->setStretchFactor(1, 1); overallLayout->addWidget(m_splitter);
I know there is some simplification that can be done. That will happen once this is working. Any suggestions on either problem listed above would be appreciated.
-
@james-b-s
The problem turned out to be the initialization of overallLayout. The parent was not passed in. The interesting thing is that the dialog worked fine without the parent until I added the splitter and the right side. Evidently, there is something about the splitter that requires that its parent have a valid parent. The problem wasn't even in any of the code that I was adding.This is version qt version 5.12
-
Hi,
Can you provide a complete minimal compilable example that shows your issue ?
Also, which version of Qt are you using ?
On which platform ?On a side note, since you are putting all your widgets in layouts or the splitter, there's no need to give them parent at construction time as they are going to be reparented anyway when setting them in said layouts or QSplitter.
-
@james-b-s said in Problem with splitters and layouts:
- When I display the dialog, it is displayed too small. I can manually expand it and it has all the widgets, but the initial display is not big enough to show most of the left side and bottom of the stuff to the left of the splitter, and the right side is not displayed at all. Until I manually expand it.
Is
overallLayout
the actual base layout of your dialog? From your description one can assume that you have elements floating around which are not resized with the dialog. -
@james-b-s I was having similar difficulties some time ago but solved the problem by using
QFrame
's to hold the layouts for the individual panels. I'm not sure that it makes any difference, though. My problem was that I was trying to add the layouts directly to the splitter, which is not allowed according to the documentation forQSplitter
.Another thing I noticed from the code you have shown is that you create the widgets with the splitter as parent. I would not do this because when you call
QSplitter::addWidget()
, the splitter automatically becomes the parent of the widget given.Try setting it up in Qt Designer and look at the generated code?
-
@james-b-s
The problem turned out to be the initialization of overallLayout. The parent was not passed in. The interesting thing is that the dialog worked fine without the parent until I added the splitter and the right side. Evidently, there is something about the splitter that requires that its parent have a valid parent. The problem wasn't even in any of the code that I was adding.This is version qt version 5.12
-
-
@james-b-s said in Problem with splitters and layouts:
The problem turned out to be the initialization of overallLayout. The parent was not passed in
That's what I said here:
@Pl45m4 said in Problem with splitters and layouts:
Is overallLayout the actual base layout of your dialog? From your description one can assume that you have elements floating around which are not resized with the dialog.
-> the layout was not applied to the dialog either with
setLayout(overallLayout);
or by making the dialog a direct parent of the main layout (i.e.overallLayout
).