[英]Change layout content with pointers in Qt
Is it possible to change the content of a layout with pointers? 是否可以使用指针更改布局的内容?
I want to do something like the following. 我想做类似以下的事情。
In the header: 在标题中:
QWidget* activeView;
QWidget* one;
QWidget* two;
...
In the source file I set up the Widgets in the constructor, and push the first to the layout: 在源文件中,我在构造函数中设置了Widget,然后将第一个推送到布局:
activeView = one;
ui->activeLayout->addWidget(activeView);
And later with slots or methods I want to change the activeView
: 后来有了插槽或方法,我想更改
activeView
:
activeView = two;
I tried this, but it won't work. 我试过了,但是行不通。 I even called the
update()
method of the layout. 我什至调用了布局的
update()
方法。
This will not work unfortunately. 不幸的是,这将无法正常工作。
What you want is QStackedWidget
. 您想要的是
QStackedWidget
。 Official documentation is here . 官方文档在这里 。
If you don't want to use this widget and want to do it manually, you will have to reconstruct the layout by yourself in code. 如果您不想使用此小部件并希望手动进行操作,则必须自己在代码中重建布局。 It can be wrapped in a method and won't be too ugly.
它可以包装在一个方法中,不会太难看。
Example: 例:
void clearLayout(QLayout *layout)
{
if (layout) {
while(layout->count() > 0){
QLayoutItem *item = layout->takeAt(0);
delete item->widget();
delete item;
}
}
}
void setNewWidgetVisible(QWidget *activeView)
{
clearLayout(ui->activeLayout);
ui->activeLayout->addWidget(activeView);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.