简体   繁体   English

如何从主布局中删除所有项目?

[英]How to remove all items from main layout?

I have MainWindow with QWidget *mainWidget. 我有MainWindow和QWidget * mainWidget。 This widget has setting layout - QLayout *mainLayout. 这个小部件有设置布局 - QLayout * mainLayout。 When I will press button "Next" I want to clear mainLayout from all items (I need use mainLayout again but with diffrent items) and also when I will press button "Back" again, I need clear layout and use it with another items. 当我按下“下一步”按钮时,我想从所有项目中清除mainLayout(我需要再次使用mainLayout但是使用不同的项目),当我再次按下“返回”按钮时,我需要清晰的布局并将其与其他项目一起使用。

I tried use while loop with mainWidget->layout()->TakeAt(0) but it doesn't work (I think that I have problem with while loop because my mainLayout has a few columns and rows). 我尝试使用while循环与mainWidget-> layout() - > TakeAt(0),但它不起作用(我认为我有问题与while循环,因为我的mainLayout有几个列和行)。 I am begginer and I have problems with understand memory allocation etc. 我很沮丧,我有理解内存分配等问题。

Delete all widgets: they remove themselves from the layout automatically. 删除所有小部件:它们会自动从布局中删除自己。 You can also set a different layout on the main widget, then add new widgets to it. 您还可以在主窗口小部件上设置不同的布局,然后向其添加新的窗口小部件。 When moving to the previous page, set the previous layout on it. 移至上一页时,请在其上设置上一个布局。 You'd have to manage the visibility of the widgets, though: they'd still be visible. 但是,您必须管理小部件的可见性:它们仍然可见。

But none of this is necessary: each page should be its own widget, that you parent to the main widget when you wish it shown, and un-parent when it's to be hidden. 但这些都不是必需的:每个页面都应该是它自己的小部件,当你希望它显示时你是主小部件的父级,而当它被隐藏时是非父级的。 The main layout can then be anything simple, like QVBoxLayout , since it holds only one widget – the current page. 主要布局可以是任何简单的,如QVBoxLayout ,因为它只包含一个小部件 - 当前页面。

And then: are you sure there's no widget or layout that supports pages of widgets? 然后:你确定没有支持小部件页面的小部件或布局吗? Look in the documentation, you may be pleasantly surprised :) 看看文档,你可能会惊喜:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM