[Qt-interest] Remove "central" layout when button is pressed and populate "central" layout with new widgets
S. Aguinaga
sa_jr at yahoo.com
Tue Jul 28 18:02:45 CEST 2009
In relation to thread:
Re: [Qt-interest] Problem with adding/removing/adding elements in a QVBoxLayout
I have a window with a central layout, when I press one of the buttons in the central widget, I want to remove this layout/widget(?) and in the SLOT callback code populate the central area with a new set of Widgets (a form with QLineEdits, and labels and 1 or 2 buttons to 'save' and return'). But maybe this is in _general_ the wrong way of going about it!
I'm able to remove the central widget using either of the following lines in my callback SLOT function:
CustomCentalWidget::close();
or
CustomCentralWidget::QWidget::hide();
But if I create a new QGroupBox and a QVBoxLayout and add widgets to this layout I can't get them to show up.
References:
The button calling the _slot_ code:
CentralPage::CentralPage(QWidget *parent)
> : QWidget(parent)
>{
>....
>connect( addExperimenterButton, SIGNAL( clicked() ), this,\
> SLOT( addExperimenterSlot() ) );
>}
I have simplified the callback code to see if I can get something to show up:
bool CentralPage::addExperimenterSlot(void)
>{
> //CentralPage::QWidget::hide();
> CentralPage::close();
>
> QPushButton *addExp2dbButton = new QPushButton(this, tr("Add") );
> addExp2dbButton->show();
> //this->show();
>
>>
>
>>
> return 1;
>}
>
>
What I end up with is with an empty central widget:
+------------------------+
| north Widget Group Box |
+------------------------+
| |
| (central widget) |
| |
+------------------------+
| South Widget Group |
+------------------------+
So,
1. How can I add new widgets to the central widget from a private/public SLOT?
2. Is what I want to do the right way of going about it?
________________________________
From: Karol Krizka <kkrizka at gmail.com>
To: Juan Pablo Crossley <crossleyjuan at gmail.com>
Cc: qt-interest at trolltech.com
Sent: Wednesday, May 20, 2009 12:05:21 AM
Subject: Re: [Qt-interest] Problem with adding/removing/adding elements in a QVBoxLayout
On Tue, 2009-05-19 at 11:33 -0500, Juan Pablo Crossley wrote:
> Finally!
>
> I solved this, the problem was with the memory deletion of the child
> widget,
>
> QVBoxLayout* box = (QVBoxLayout*)layout();
>
> QLayoutItem* item;
> while ((item = box->takeAt(0)) != 0) {
> box->removeWidget(item->widget());
> delete item->widget(); <<<---- very important step!!!!
> }
>
> After removing the widget using the "removeWidget" you must delete the
> item, so I combined the removeWidget with the delete and it did the
> trick.
>
You might want to do:
item->widget()->deleteLater();
This deletes the widget next time Qt events are processed, in case there
are stale events from the widget. Not doing so sometimes leads to the
program crashing, at least according to my experience.
--
Cheers,
Karol Krizka
http://www.krizka.net
_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090728/8e3823e1/attachment.html
More information about the Qt-interest-old
mailing list