PDA

View Full Version : delete layout() / setLayout issue



vlg789
21st September 2007, 14:13
hello everyone,
i have a little problem and i cand't find the problem.
(all code it's in the constructor of a class that extends QWidget)

mainLayoutBigg = new QGridLayout((QWidget*)this);
mainLayoutSmall = new QGridLayout((QWidget*)this);

mainLayoutBigg->addWidget(qwd_1,...);
mainLayoutBigg->addWidget(qwd_2,...);
mainLayoutBigg->addWidget(qwd_3,...);

mainLayoutSmall->addWidget(qwd_1,...);
mainLayoutSmall->addWidget(qwd_2,...);

this->setLayout(mainLayoutSmall);
delete layout();
this->setLayout(mainLayoutBigg); "]<=== here dies [/B]..., more exactly in a QWidget.cpp setParent(this) ---> QObjectPrivate::setParent_helper(QObject *o), parentD->children[index] = 0; because index is -1

what i'm trying to do is to swich between 2 layouts ... ibut i don;t know that's wrong with the code above. if anyone cand help ...

ps: Qt 4.3.1

marcel
21st September 2007, 14:37
You better use a QStackedWidget.

I think the problem is that you add widgets to both layouts, and you delete one of them. You should add the widgets to the second layout after the first one is deleted.
This code:


mainLayoutBigg = new QGridLayout((QWidget*)this);
mainLayoutSmall = new QGridLayout((QWidget*)this);
Creating the layouts with parents causes them to be set just as using setLayout.

vlg789
21st September 2007, 15:38
mainLayoutSmall=041315C8 mainLayoutBigg=04131448,

after setLayout(mainLayoutSmall), layout()=041315C8
mainLayoutSmall=041315C8 mainLayoutBigg=04131448,
after delete layout, layout()=00000000

mainLayoutSmall=041315C8 mainLayoutBigg=04131448,
after setLayout, layout(mainLayoutBigg)=04131448
mainLayoutSmall=041315C8 mainLayoutBigg=04131448,
after delete layout, layout()=00000000

so ... delete layout() deletes mainLayoutSmall and mainLayoutBigg objects but mainLayoutSmall and mainLayoutBigg pointers still pointing some adress ....

ok, how cand i switch between two layouts then ? i whant to create in constructir 2 layouts and switch between them later in the app ?

marcel
21st September 2007, 15:43
Don't delete the layouts then. Just set them.
This way your objects will remain intact.
You don't have any reason to delete the layout because QWidget does not create a copy when you set it. It uses the one you pass.



so ... delete layout() deletes mainLayoutSmall and mainLayoutBigg objects but mainLayoutSmall and mainLayoutBigg pointers still pointing some adress ....

Of course they are. Nobody makes them null.

vlg789
21st September 2007, 15:46
not so easy.... Acording to http://doc.trolltech.com/4.3/qwidget.html#setLayout, You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout

jpn
21st September 2007, 18:25
Just implement the alternating part of the layout as QStackedWidget (or QStackedLayout) and the problem is solved. This will also save you from possible upcoming flicker in case you'll get the layout switch hack working.. ;)