PDA

View Full Version : Layout changes parent by itself. How comoes?



nernst
18th December 2014, 07:06
Hi,

I am trying to put some buttons dynamically into a QGridLayout, that is in a QTabView. Therefore I put that QGridLayout into a tab and fill it with the buttons.
The problem is, that when I add more than 4 Buttons to that layout it changes its parent somwhow and I get an error. The error occurs, when the button shall be added to the layout.


Error:
parent: QWidget(0x289be8, name = "tabUserSpecificMethods")
parent: QWidget(0x289be8, name = "tabUserSpecificMethods")
parent: QWidget(0x289be8, name = "tabUserSpecificMethods")
parent: QWidget(0x289be8, name = "tabUserSpecificMethods")
parent: QWidget(0x289be8, name = "tabUserSpecificMethods")
parent: screenMeasMeth(0x260830, name = "screenMeasMeth")
QLayout::parentWidget: A layout can only have another layout as a parent.
Segmentation fault



my Code looks like that:

VLaytUserSpecificMethods = new QGridLayout(ui->tabUserSpecificMethods);
VLaytUserSpecificMethods->setHorizontalSpacing(6);
VLaytUserSpecificMethods->setVerticalSpacing(6);
VLaytUserSpecificMethods->setObjectName(QString::fromUtf8("VLaytUserSpecificMethods"));
VLaytUserSpecificMethods->setContentsMargins(10, 15, 0, 0);
VLaytUserSpecificMethods->setGeometry(QRect(0,0,431,490));

for(int i = 0; i <= tmpMethodNames.size()-1; i++){
listBtnUserMeasMeth[i] = new QPushButton(this);
listBtnUserMeasMeth[i]->setVisible(true);
listBtnUserMeasMeth[i]->setText(tmpMethodNames.at(i));
listBtnUserMeasMeth[i]->setMaximumSize(QSize(200,55));
listBtnUserMeasMeth[i]->setMinimumSize(QSize(200,55));
listBtnUserMeasMeth[i]->setStyleSheet(globalStyleSheet::btnEnabled);
listBtnUserMeasMeth[i]->setFont(dynamicBtnFont);
listBtnUserMeasMeth[i]->setFocusPolicy(Qt::NoFocus);

qDebug() << "parent: " << VLaytUserSpecificMethods->parent();
if(i % 2 == 0){
VLaytUserSpecificMethods->addWidget(listBtnUserMeasMeth[i],c_GridRowCounter,0);
}
else{
VLaytUserSpecificMethods->addWidget(listBtnUserMeasMeth[i],c_GridRowCounter,1);
c_GridRowCounter++;
}
}
VLaytUserSpecificMethods->addItem(VSpacerBtnList,++c_GridRowCounter,0);


Does anybody know the reason, why this happens?

anda_skoa
18th December 2014, 08:37
Are you sure the error happens in there?

Did you try a debug output after the loop?
What does the backtrace of the crash say?

Cheers,
_

nernst
18th December 2014, 09:41
Yes, i tried debugging after the loop. The error occurs when adding the widget to the layout (line 20 / 23).

Ok, I found a solution to go around that problem.



ui->gridLayout->setHorizontalSpacing(20);
ui->gridLayout->setVerticalSpacing(6);
ui->gridLayout->setContentsMargins(10, 15, 0, 0);

UserSpecificMethods *userSpecificMeasMethods = new UserSpecificMethods();
tmpMethodNames = userSpecificMeasMethods->readUserMeasMethodNames();
delete userSpecificMeasMethods;

for(int i = 0; i <= tmpMethodNames.size()-1; i++){
listBtnUserMeasMeth[i] = new QPushButton(this);
listBtnUserMeasMeth[i]->setVisible(true);
listBtnUserMeasMeth[i]->setText(tmpMethodNames.at(i));
listBtnUserMeasMeth[i]->setMaximumSize(QSize(200,55));
listBtnUserMeasMeth[i]->setMinimumSize(QSize(200,55));
listBtnUserMeasMeth[i]->setStyleSheet(globalStyleSheet::btnEnabled);
listBtnUserMeasMeth[i]->setFont(dynamicBtnFont);
listBtnUserMeasMeth[i]->setFocusPolicy(Qt::NoFocus);

qDebug() << /*"parent: " << /*VLaytUserSpecificMethodsui->gridLayout->parent() <<*/ "\n vektor: " << listBtnUserMeasMeth[i];
if(i % 2 == 0){
ui->gridLayout->addWidget(/*vectorUserMeasMethodBtns[i]*/listBtnUserMeasMeth[i],c_GridRowCounter,0);
}
else{
ui->gridLayout->addWidget(/*vectorUserMeasMethodBtns[i]*/listBtnUserMeasMeth[i],c_GridRowCounter,1);
c_GridRowCounter++;
}
}
QSpacerItem *VSpacerBtnList = new QSpacerItem(10,10,QSizePolicy::Expanding,QSizePoli cy::Expanding);
ui->gridLayout->addItem(VSpacerBtnList,++c_GridRowCounter,0);

I only put the QGridLayout in the Tab with the QtDesigner and add the buttons like before. This works very nice. The only thing is, that when I want to reload the tab, I have to remove the Buttons from the layout before, so that they won't be put twice to the QGridLayout.

It works very well.