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

来源:互联网 发布:l300清零软件 编辑:程序博客网 时间:2024/05/19 23:25

        其实很简单,但是对于我来说解决了一个一直困扰我的问题。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


0 0
原创粉丝点击