Hi,
I'm trying to create a GUI which has three buttons:
- connectButton: Connects to a SQLite3 database file on my computer.
- showTableButton: Shows a table from the database file in a QTableView.
- disconnectButton: Disconnects from the database file so I can edit it externally without closing the QT GUI.
The code for each button is:
on_connectButton_clicked():
db.setDatabaseName("metadata.db");
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "metadataDB");
db.setDatabaseName("metadata.db");
To copy to clipboard, switch view to plain text mode
on_showTableButton_clicked():
model->setTable("TableName");
model->select();
view->setModel(model);
view->show();
QSqlDatabase db = QSqlDatabase::database("metadataDB");
QSqlTableModel *model = new QSqlTableModel(this,db);
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->setTable("TableName");
model->select();
QTableView *view = new QTableView;
view->setModel(model);
view->show();
To copy to clipboard, switch view to plain text mode
on_disconnectButton_clicked()
db.close();
QSqlDatabase db = QSqlDatabase::database("metadataDB");
db.close();
QSqlDatabase::removeDatabase("metadataDB");
To copy to clipboard, switch view to plain text mode
The program connects to the database and displays the table correctly. However disconnectButton() doesn't release the metadata.db file. So if I try and delete or rename the file I get "the process cannot access the file because it is being used by another process". If I close the QT GUI then metadata.db is accessible again. Similarly if I never call showTableButton then metadata.db is always accessible.
If anyone could point out what I'm doing wrong I would greatly appreciate it. I suspect I need to delete the QTableModel but I'm not sure how to get a handle for it from inside disconnectButton().
Regards,
Robbie
Bookmarks