Qt:如何在关闭widget时自动释放内存


今天在调试程序的时候发现一个问题,调用close槽函数关闭widget后,反复重新打开这个widget会变得很慢,查看资源管理器发现进程的内存未被释放。之前一直以为调用close函数后new的对象都会被自动delete掉,看来不是这样。

查看了qt帮助文档,

QWidget::close () [slot]

有这么一段话

    First it sends the widget a QCloseEvent. The widget is hidden if it accepts the close event. 
    If it ignores the event, nothing happens. The default implementation of QWidget::closeEvent() accepts the close event.
    If the widget has the Qt::WA_DeleteOnClose flag, the widget is also deleted.    

所以只要给widget设置Qt::WA_DeleteOnClose的属性就好啦

Widget->setAttribute(Qt::WA_DeleteOnClose);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM