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