#include "qtmemleak2.h" QtMemLeak2::QtMemLeak2(QWidget *parent) : QMainWindow(parent) { treeView.setModel(&standardModel); connect(&timer, SIGNAL(timeout()), this, SLOT(switchModel())); timer.start(0); } // deletes old selection model, as described in docs http://doc.qt.io/qt-5/qabstractitemview.html#setModel void QtMemLeak2::mySetModel(QAbstractItemModel *model) { QItemSelectionModel *m = treeView.selectionModel(); treeView.setModel(model); delete m; } void QtMemLeak2::switchModel() { mySetModel(NULL); mySetModel(&standardModel); }