Reputation: 399
I have class which is derived from QSqlRelationalTableModel
.
I use SQLite database.
And I need to change database file.
I close old database file, call SetDatabaseName("path to new file") and open new database file.
Now I just call select() for the model, but it returns false.
And if I call setTable("table") and only after that select(), everything works...
But name of the table is the same...
I didn't find any method which allows to inform a model that database connection has been changed.... Do you know a better way to inform the model?
Ok. I have returned to this topic once again.
After db is changed I have to call setTable() with the same table name to reinit table model. And I did not find a better way how to reinit table view, co I call
pTableView->setModel(NULL);
pTableView->setModel(model);
This generates a lot of unnecessary code calls, but in other case table view does not know about changes in table model (for example columns count).
I did not find a better way to reinitialize QSqlTableModel
and QTableView
. Some ideas?
Upvotes: 1
Views: 2948
Reputation: 2702
// create your model(parent=0, QSqlDatabase::database());
// set your table model->setTable("tablename");
tableview->setModel(this->model)
bool ok = model->select() // model should populate itself and refresh tableView as well
Upvotes: 2
Reputation: 399
That was my fault :( With help of sources I found in which cases select() returns false. And one fieldname in the table of my new database misses one letter:( So, QSqlQuery with select returned error. setTable call updates fieldnames of the table, so select returns true.
Upvotes: 0