PDA

View Full Version : how to access layout within another layout in qt



lakeat
5th January 2013, 05:20
I got an error when running my qt program. (It used to work and can be compiled perfectly before I add this new function void perfectPanel::aaaf in order to do something.) The error message is,


QLayout::parentWidget: A layout can only have another layout as a parent. Segmentation fault (core dumped)

It happens since I was trying to add a new widget to my layout gridLayoutSub_, but layout gridLayoutSub_ is within another layout, let's call it gridLayout_


void perfectPanel::aaaf()
{
// some codes ...
gridLayoutSub_->addWidget(kkk, 6, 0, 1, 1);
}

Anyone knows what's going wrong and how to fix it?

More details: Declarations of these two layouts

In mymainwindow class, there is a dock widget. And in order to customize this dock widget, I wrote a new class called perfectPanel which is a subclass of QWidget, and then in the constructor of this perfectPanel, I have the following codes:


groupBoxA = new QGroupBox(tr("groupBoxA"));
QGridLayout* gridLayoutA = new QGridLayout(groupBoxA);

VBoxLayoutPanel = new QVBoxLayout(this);
VBoxLayoutPanel->addWidget(groupBoxA);

groupBoxA->setLayout(gridLayoutA);
setLayout(VBoxLayoutPanel);
As you can see, gridLayoutA is within another layout VBoxLayoutPanel. And the error I mentioned happens when I try to addWidget to gridLayoutA later in function void perfectPanel::aaaf, during:


gridLayoutA->addWidget(kkk, 6, 0, 1, 1);

The header of perfectPanel class


class perfectPanel : public QWidget
{
Q_OBJECT

public:

explicit perfectPanel(QWidget *parent = 0);
~perfectPanel();

private:
// ...
}

Santosh Reddy
5th January 2013, 07:07
Everything looks fine, it should just work. I think there somthing else you are doing, which missed to mention.

lakeat
7th January 2013, 20:31
I found if I VBoxLayoutPanel.addWidget(), this works. So I am wondering what's the reason that i can only addWidget to parent layout not sub-layout?

Santosh Reddy
8th January 2013, 06:57
Can you reproduce the warning with a sample project?