layout中的控件可以通过addWidget添加。但是有个问题:增加之后如何将控件删除呢,并且使其立即生效是一个问题。

实现方法:

QWidget有一个setParent方法,当setParent(NULL)时,就会使其不在相应的界面上显示。如果不设置,即便删除了layout,QWidget还是会显示在界面上。

void QWidget::setParent(QWidget *parent)

Sets the parent of the widget to parent, and resets the window flags. The widget is moved to position (0, 0) in its new parent.

If the new parent widget is in a different window, the reparented widget and its children are appended to the end of the tab chain of the new parent widget, in the same internal order as before. If one of the moved widgets had keyboard focus, setParent() calls clearFocus() for that widget.

If the new parent widget is in the same window as the old parent, setting the parent doesn't change the tab order or keyboard focus.

If the "new" parent widget is the old parent widget, this function does nothing.

Note: The widget becomes invisible as part of changing its parent, even if it was previously visible. You must call show() to make the widget visible again.

Warning: It is very unlikely that you will ever need this function. If you have a widget that changes its content dynamically, it is far easier to use QStackedWidget.

[pure virtual] QLayoutItem *QLayout::takeAt(int index)

Must be implemented in subclasses to remove the layout item at index from the layout, and return the item. If there is no such item, the function must do nothing and return 0. Items are numbered consecutively from 0. If an item is removed, other items will be renumbered.

The following code fragment shows a safe way to remove all items from a layout:

QLayoutItem *child;while ((child = layout->takeAt(0)) != 0) {...delete child;}

方法一:

代码实现:

/清空horizontalLayout布局内的所有元素
QLayoutItem *child;while ((child = horizontalLayout->takeAt(0)) != 0){//setParent为NULL,防止删除之后界面不消失if(child->widget()){child->widget()->setParent(NULL);}delete child;}

方法二:

代码实现

QList<SystemMenuButton*> btns = menulayout->findChildren<SystemMenuButton*>();//获取布局中所有按钮foreach (SystemMenuButton *btn, btns) {delete btn;    //析构所有按钮}

--------------------- 
作者:itas109 
来源:CSDN 
原文:https://blog.csdn.net/itas109/article/details/83181576 
版权声明:本文为博主原创文章,转载请附上博文链接!

Qt中清空layout中所有控件相关推荐

  1. Qt 在designer 中给QToolBar 添加 action控件

    Qt 在designer 中给QToolBar 添加 action控件 1.在action editor 添加控件 SendData不出现使用打钩时 2. 使用鼠标直接拖拽到Ui指定位置

  2. android tv 开发布局,Android TV开发总结(七)构建一个TV app中的剧集列表控件

    前言:剧集类控件,在TV app中非常常见,今天将介绍构建一个TV app中的剧集列表控件,此控件上传到我的Github:https://github.com/hejunlin2013/Episode ...

  3. delphi中利用Indy的TIdFtp控件实现FTP协议

    2019独角兽企业重金招聘Python工程师标准>>> delphi中利用Indy的TIdFtp控件实现FTP协议版权声明:本文为博主原创文章,未经博主允许不得转载.现在很多应用都需 ...

  4. 在VS2010/MFC中如何对对话框添加控件

    创建对话框资源需要创建对话框模板.修改对话框属性.为对话框添加各种控件等步骤.而如何为对话框添加控件直接影响到界面的美观和开发人员的工作效率.本人将为大家介绍如何为对话框添加控件的详细步骤. 方法/步 ...

  5. WPF 中动态创建和删除控件

    WPF 中动态创建和删除控件 原文:WPF 中动态创建和删除控件 动态创建控件 1.容器控件.RegisterName("Name",要注册的控件)   //注册控件 2.容器控件 ...

  6. MFC中如何让一个CStatic控件响应消息

    在MFC中有时需要自己Create控件,如何写一个可以响应消息的控件呢,例如:CStatic 如下: CStatic test; test.Create("my static",  ...

  7. 弹出框页面中使用jquery.validate验证控件

    弹出框页面中使用jquery.validate验证控件有几个问题需要解决: 1,弹出框的提交事件完成后如何关闭弹出框页面? 2,提交不成功如何返回当前页? 3,如果知道验证事件成功? 之前笔者都是JS ...

  8. Delphi 7 中使用IdUDPServer1和IdUDPClient1控件实现通信检测

    Delphi 7 中使用IdUDPServer1和IdUDPClient1控件实现通信,可是如何检测两台电脑已经连通了?代码怎么写啊?求大神 D7确实经典,但已经落伍了,用xe版本吧 udp发送这个东 ...

  9. VS2010中如何实现自定义MFC控件

    本文简要讲解在VS2010中怎样实现自定义MFC控件的知识,以下是分步骤说明. 一.自定义一个空白控件  1.先创建一个MFC工程 NEW Project-->MFC-->MFC Appl ...

  10. Web开发中的弹出对话框控件介绍

    Web开发中,目前由于Jquery的大行其道,因此很多弹出对话框,都用到了Jquery技术,反而原始的弹出对话框的方式较为少用了.不过基于JQuery的方式实现对话框窗口弹出,也有很多控件可以利用,由 ...

最新文章

  1. java程序设计B_Java程序设计11——GUI设计与事件处理B
  2. HTML 介绍及标签
  3. [leetcode] 62 Unique Paths (Medium)
  4. 2017-9-17pat甲级 B
  5. android:ellipsize = marquee 跑马灯,Android-单行跑马灯(一直循环)效果
  6. 使用SonarCloud对.NET Core项目进行静态代码分析
  7. CSS padding margin border属性讲解
  8. 团队-爬虫豆瓣top250项目-模块开发过程
  9. linux同时挂载多个iso,Linux 挂载iso,并设置为源
  10. 夺命雷公狗---DEDECMS----23dedecms修改内容页面展示的信息
  11. 阿里云申请商标注册步骤详细教程
  12. Microsoft Store无法打开几种解决办法
  13. 广告机-开机自动播放-视频-电影-图片-竖屏-分屏-展示机
  14. linux samba 漏洞 exp,smaba漏洞总结
  15. Synchronized和Reentrantlock的区别
  16. 【VUE】实现自动滚动
  17. Unity工程里的那些东西
  18. 成为一个15K的软件测试工程师的基本要求
  19. 第133天学习打卡(Docker 初识Docker Docker的基本组成 安装docker 阿里云镜像加速 Docker常用命令)
  20. 物联网市场FD-SOI制程会取代FinFET吗?

热门文章

  1. 移动端html页面显示图表,HTML5移动端数据图表组件调研
  2. html5shiv_深入探讨:HTML5 Shiv和Polyfills
  3. 【Web理论篇】Web应用程序安全与风险
  4. Linux高手必看的10本经典书籍
  5. linux如何从 命令行 将普通文件打印到 pdf
  6. vgg16 清华镜像_Python models.vgg16方法代码示例
  7. 微信小程序上传照片加水印
  8. ssm毕设项目疫情防控管理系统02vsf(java+VUE+Mybatis+Maven+Mysql+sprnig)
  9. 看!Mac上好用的流程图软件就是它
  10. 电子书格式转换 azw3 to epub (软件calibre的简单使用)