Funt
Funt

Reputation: 399

How to update QSqlTableModel, QTableView after database connection has been changed

I have already asked before, but now I have more questions.

So, I have QTableView and QSQLTableModel.

pTableModel->setTable("table_name");
pTableView->setModel(pTableModel);

In QTableView I need to show only column with specified names, I use setColumnHidden() for this.

Now I need to change DB and show specified columns of the table with the same name. What is better way to inform table view and table model that DB has been changed. Table in the new DB can differs by column count, row count but has all the necessary columns.

Now I call

pTableModel->setTable("table_name");

once again with the same table name to update table model. And call

pTableView->horizontalHeader()->doItemsLayout();

to inform table view about possible change of columns count.

It seems to be ugly and I am not sure that all the necessary data have been updated. Do you know a better way?

Upvotes: 1

Views: 1121

Answers (1)

e-zinc
e-zinc

Reputation: 4581

In my opinion you have to instantiate new QSQLTableModel for new database connection and assign it to pTableView. I thinks it is more safe.

Upvotes: -1

Related Questions