今天在调试程序的时候发现一个问题,调用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);