今天在調試程序的時候發現一個問題,調用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);