Your QSqlTableModel is owned by the MainWindow object and will be automatically destroyed when this is closed (line 56)
the QTableView and QHeaderView have no parents and will need to be deleted explicitly.
By deleting the model in line 40 you are unbalancing the call stack which will make the application crash.
Putting delete operators inside an if statement is rarely a good idea, if they are used at all they should unconditionally delete the object created by new when the object is no longer wanted.
A better solution would be to give the table and hearde views parents and let QT deal with the garbage collection.
Bookmarks