[SOLVED] Clear widget layout and repaint with a new one
-
Hey everybody!
I have a widget with a set layout at the start of the application. I want to repaint the widget with a new layout on the click of a button. I tried the solutions given "here":http://www.qtforum.org/article/17819/clear-a-widget-and-repaint-with-a-new-layout-solved.html?s=f39f70e41208b92024d876f0438d85222a635871#post69511 and "here":http://stackoverflow.com/questions/4857188/clearing-a-layout-in-qt but it was of no avail.
Please help! Thanks.
-
If you don't plan on using the same items in the new layout you can use the QStackedLayout class. Otherwise create a new layout, add your items to the new layout, delete whatever is remaining of the first layouts items, delete the widget->layout() and then call widget->setLayout with the new layout you created.
-
[quote author="b1gsnak3" date="1360229661"]Otherwise create a new layout, add your items to the new layout[/quote]
That's what I tried to do but the layout is not getting cleared. Here's my code
@
QHBoxLayout *hlayout = new QHBoxLayout();
QVBoxLayout *vlayout = new QVBoxLayout();hlayout->addWidget(label1);
hlayout->addWidget(label2);MainLabel->setLayout(hlayout);
if(keyEvent->key()==Qt::Key_Down) //If the down arrow key is press
{
delete MainLabel->layout();
vlayout->addWidget(label3);
vlayout->addWidget(label4);
MainLabel->setLayout(vlayout);
}
@ -
Well, it's not cleared because you are using different items (label3, 4). If you wanted the same items (label1, 2) then it would've been cleared. However, for this, you should use QStackedLayout and change the index of the layout (0 for your hLayout and 1 for vLayout, for example). If you really want to use this model, then try using:
@{
delete MainLabel->layout();
qDeleteAll(MainLabel->children());
label3 = new QLabel();
label4 = new QLabel();
etc.
}@ -
Since the layout is itself a child of the widget it manages, you should be able to just use qDeleteAll(MainLabel->children()) and drop the delete MainLabel->layout() call.
-
Aqui eu limpei o QFrame para receber novos objetos, meu QFrame Pai se chama "frame_104" e o QFrame filho se chama "frame_grafico", neste exemplo eu preciso limpar o frame filho para atualizar meu grafico que fica dentro dele,sendo assim:
QFrame frameGrafico = ui->frame_104->findChild<QFrame>("frame_grafico");// Remover todos os widgets filhos do frameGrafico
QList<QWidget*> children = frameGrafico->findChildren<QWidget*>();
for (QWidget* child : children) {
child->setParent(nullptr); // Desanexar o widget do pai
child->deleteLater(); // Marca o widget para ser deletado mais tarde de forma segura
}// Remover layout antigo, se existir
if (frameGrafico->layout() != nullptr) {
QLayout *oldLayout = frameGrafico->layout();
frameGrafico->setLayout(nullptr);
delete oldLayout;
} -
@Igor-Gomes hi and welcome to devnet,
Thanks for participating but please use English as this is the official language of the forum. There is an international sub-forum where you can use your mother tongue.