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);
}
void perfectPanel::aaaf()
{
// some codes ...
gridLayoutSub_->addWidget(kkk, 6, 0, 1, 1);
}
To copy to clipboard, switch view to plain text mode
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:
VBoxLayoutPanel->addWidget(groupBoxA);
groupBoxA->setLayout(gridLayoutA);
setLayout(VBoxLayoutPanel);
groupBoxA = new QGroupBox(tr("groupBoxA"));
QGridLayout* gridLayoutA = new QGridLayout(groupBoxA);
VBoxLayoutPanel = new QVBoxLayout(this);
VBoxLayoutPanel->addWidget(groupBoxA);
groupBoxA->setLayout(gridLayoutA);
setLayout(VBoxLayoutPanel);
To copy to clipboard, switch view to plain text mode
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);
gridLayoutA->addWidget(kkk, 6, 0, 1, 1);
To copy to clipboard, switch view to plain text mode
The header of perfectPanel class
class perfectPanel
: public QWidget{
Q_OBJECT
public:
explicit perfectPanel
(QWidget *parent
= 0);
~perfectPanel();
private:
// ...
}
class perfectPanel : public QWidget
{
Q_OBJECT
public:
explicit perfectPanel(QWidget *parent = 0);
~perfectPanel();
private:
// ...
}
To copy to clipboard, switch view to plain text mode
Bookmarks