Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - change QWidget layout

Let's consider we have a QWidget and a QLayout named general_layout that contains other widgets and layouts. general_layout is set as the QWidget layout like this:

setLayout(general_layout)

Now I should to change the content of QWidget. How can I do that? I have tried to delete and create a new layout for the QWidget and that new layout set as a layout of the QWidget, but could not complete my intentions successfully.

This is my code:

delete general_layout;
general_layout = new QHBoxLayout;
general_layout->addLayout(some_layout);
myQWidget->setLayout(general_layout);
like image 452
Narek Avatar asked Jun 07 '10 14:06

Narek


People also ask

How do I change the layout of QWidget?

delete general_layout; general_layout = new QHBoxLayout; general_layout->addLayout(some_layout); myQWidget->setLayout(general_layout); qt. layout. qt4.

How do you change geometry in Qt Designer?

You can override the defaults by clicking on the parent widget of the layout, and scrolling down to the bottom of the Property Editor, where you'll see a Layout section with the relevant values ( -1 means "use the default").

How do I replace a widget?

First, tap-and-hold on a widget to grab it. You can see the Remove option displayed at the top of the screen. Move your finger to drag the widget onto Remove. Once you have it there, lift your finger.

How do I add a button to layout Qt?

You can add QPushbutton to central widget by passing its object name. QPushButton *PB=new QPushButton(ui->centralwidgetobjectname); PB. show(); Otherwise create layout inside Central widget then using addwidget function to add Qpushbutton.


1 Answers

The problem is that the widgets of a layout are not destroyed when deleting a layout. This results in all child widgets of myQWidget still being present, be it without a layout.

The solution is simple: add a

qDeleteAll(myQWidget->children());

after

delete general_layout;
like image 197
mtvec Avatar answered Sep 20 '22 19:09

mtvec