1 Attachment(s)
Disconnecting from database after using QSqlTableModel
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():
Code:
db.setDatabaseName("metadata.db");
on_showTableButton_clicked():
Code:
model->setTable("TableName");
model->select();
view->setModel(model);
view->show();
on_disconnectButton_clicked()
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
Re: Disconnecting from database after using QSqlTableModel
did you try to do something like this?
Re: Disconnecting from database after using QSqlTableModel
Quote:
Originally Posted by
spirit
did you try to do something like this?
Hi spirit,
Thanks for the suggestion. Alas that doesn't work. Neither does redefining db.setDatabaseName(). I do suspect it will be something simple like that!
Re: Disconnecting from database after using QSqlTableModel
I don't look to your file only to this code on page, have you use somewhere
Re: Disconnecting from database after using QSqlTableModel
Quote:
Originally Posted by
mazurekwrc
I don't look to your file only to this code on page, have you use somewhere
You're right that I did forget that line. Unfortunately inserting it doesn't resolve my problem. :(
Re: Disconnecting from database after using QSqlTableModel
Quote:
Originally Posted by
RobbieClarken
You're right that I did forget that line. Unfortunately inserting it doesn't resolve my problem. :(
that's not an error, because you use QSqlDatabase::database and this method automatically opens database, from docs.
Quote:
QSqlDatabase QSqlDatabase::database ( const QString & connectionName = QLatin1String( defaultConnection ), bool open = true ) [static]
Re: Disconnecting from database after using QSqlTableModel
maybe try in on_disconnectButton_clicked first delete model and after that close and remove database