PDA

View Full Version : Reorganize elements in a QGridLayout



moijhd
26th March 2013, 09:23
Hi,

I have a QGridLayout in a Widget. This layout is divided into four parts by placing four widgets in it.

I want to be able to remove the four internal widgets and the just put one of them in the layout (which will occupy the whole space).

I am currently using the following code to remove the elements in the layout (without destroying them because I want to reuse them later) :


QLayoutItem *child;
while((child = this->gridLayout->takeAt(0)) != 0);

And then I add the new widget in the layout.

My problem is that the old items are still visible and the new one is just displayed on them...I don't understand why. The old widgets are displayed but they don't follow the layout rules (like they are not in it, they are just still "present" in the display).

Any leads ?

Thanks

PS 1 : The widget are actually QwtPlot and here is what I see on the scale for example : 8858
PS 2 : Also, the same thing is observed when I delete the layout and create a new one. Some how, the old one is still visible.
PS 3 : The problem can be seen with this code


QWidget *w = new QWidget();
w->setLayout(new QGridLayout());
static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 0, 0);
static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 0, 1);
static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 1, 0);
static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 1, 1);
delete w->layout();
w->setLayout(new QGridLayout());
static_cast<QGridLayout*>(w->layout())->addWidget(new QLabel(QString("hey")), 0, 0);
w->show();

Running that displays the label in the new label (in place) but there is still one old button in the background (it is unclickage, it is just drawn)...

Santosh Reddy
26th March 2013, 09:54
Looks you like you got most of the things correct, only thing missing is that you need to hide the widget.

Removing the widget from the layout will only remove the widget from the layout (will not hide it), the removed widget will still be a child of the layout's parent widget, which means that the removed widget is not in layout but is still in it's parent widget, hence will not follow the layout. (which is what you are seeing)

moijhd
26th March 2013, 10:48
Thanks, so the following code works :


QWidget *w = new QWidget();
w->setLayout(new QGridLayout());
static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 0, 0);
static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 0, 1);
static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 1, 0);
static_cast<QGridLayout*>(w->layout())->addWidget(new QToolButton(), 1, 1);
for(int i = 0; i < w->layout()->count(); i++)
{
w->layout()->itemAt(i)->widget()->hide();
}
delete w->layout();
w->setLayout(new QGridLayout());
static_cast<QGridLayout*>(w->layout())->addWidget(new QLabel(QString("hey")), 0, 0);
w->show();

I'll try in my case :)

PS : this also works if I don't delete and create a layout.

Added after 9 minutes:

Okay it seems to work so far.

One more thing. Of course, I don't have only widgets in my layout but I also have sub layouts. So I have to hide the widgets they contain so that they can them self get reduced to 0 ? Or is there an alternative method (except putting the sub layout in a widget and add the widget instead of the layout...) ?