Qt 将一个Widget(子)从某个Widget(父)中移除的方法

        其实很简单,但是对于我来说解决了一个一直困扰我的问题。Qt很多Widget并没有提供delete或者remove的方法。 

        如果不需要子Widget时,直接delete,用deleteLater()比较好。如果还需要使用子Widget,使用setParent()可以让子Widget从父Widget中剥离出来。无论是直接delete,还是setParent,都会通知父Widget,父Widget都会更新自己的内容。

           下面我把英文原文和相关链接附上。


It's not clear to me if you want to preserve the widget and put it somewhere else, or if you want to destroy the widget.

  • Destroying the widget: If you can get a pointer to the widget, you can simply delete it. The splitter willsafely be notified that its child isbeing deleted and will remove itfrom itself.

  • Preserving the widget: If you grab the pointer to the widget, you can simply set its parent to some other widget and add it to another widget's layout and it will show up there. This is safe because the QSplitter will be notified that one of its children is being reparented.

If you want to set the parent to NULL (cjhuitt's answer) be aware that you are now responsible for cleaning up that memory because the widget no longer has a parent.


相关链接:http://stackoverflow.com/questions/371599/how-to-remove-qwidgets-from-qsplitter

                    http://stackoverflow.com/questions/20328437/remove-widget-from-qsplitter


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值