PDA

View Full Version : QTableView : How to refresh the view after an update of the model ?



marvaneke
26th July 2006, 10:27
Hi,

I have :
QTableView *PersonTableView;
int m_PersonLine;

with these instructions, I can update the name of the person :
QAbstractItemModel *aim = PersonTableView->model();
aim->setData(aim->index(m_PersonLine, 1), sr.field("name").value().toString());
aim->submit();

Because I do not see the modification on the PersonTableView, I have to quit the application and restart it to view the modification of the name of the person in the PersonTableView.

Can someone post the code to tell to the PersonTableView that his model has changed and that it has to repaint or refresh or update or something else, the view.
Please post the code and not only the theory, because I am a newbie.

Regards.

wysota
26th July 2006, 10:33
What kind of model is this? Your own? If so, you should emit dataChanged() signal from within setData().

marvaneke
26th July 2006, 10:46
Thanks wysota for the (too ;) ) quick answer.

The model of the PersonTableView is the code below :
model->setTable("person");
model->setEditStrategy(QSqlTableModel::OnRowChange);
model->setSort(1, Qt::AscendingOrder);
model->setSort(2, Qt::AscendingOrder);
model->select();
PersonTableView->setModel(model);

I have tried the code below :
aim->dataChanged(aim->index(m_PersonLine, 0), aim->index(m_PersonLine, 2));
But the compiler answer me that it was "protected".

Regards.

wysota
27th July 2006, 13:29
You can always do model->select() after model->setData(), but it should be done automatically.

marvaneke
27th July 2006, 19:10
Hi,

with the instruction :
PersonTableView->model()->select();
I receive the error :
error: ‘class QAbstractItemModel’ has no member named ‘select’

Have an idea ?

Regards.

wysota
27th July 2006, 19:47
Sure


QSqlQueryModel *qmodel = dynamic_cast<QSqlQueryModel*>(PersonTableView->model());
if(qmodel) qmodel->select();

Alternatively use qobject_cast instead of dynamic_cast if your compiler doesn't support dynamic casting.

marvaneke
27th July 2006, 22:20
Hi wysota,

With QSqlQueryModel, it does not work, but with QSqlTableModel, it works. (As you see, with your advice, I learn. :cool: )
This line of code is working:
QSqlTableModel *stm = dynamic_cast<QSqlTableModel*>(PersonTableView->model());
Or this line of code is also working:
QSqlTableModel *stm = qobject_cast<QSqlTableModel*>(PersonTableView->model());
The next line is working :
if(stm) stm->select();

But, it as no effect. I need always to quit the application and to start it again to view the modification of the data showing in the TableView.

Have I to specify something on the View to refresh it, or something like that ?

Regards.

wysota
27th July 2006, 23:22
No, it should get refreshed by itself. But as a last resort (although I think there might be a problem with some other part of your code or with the dbms), try QAbstractItemModel (just remember it is protected). Does the dbms actually get updated when you edit the item? Please note that with the edit strategy you chose, the dbms will get updated only when you change the current row in the view.

ghorwin
7th November 2006, 03:01
I simply set the model again in table view using setModel(). Don't know whether this is most efficient but it works...

wysota
7th November 2006, 15:10
I simply set the model again in table view using setModel(). Don't know whether this is most efficient but it works...

That's the worst you can do, as all the model-view framework is then reset. It's even worse than if you would call reset() on the model - the result would be the same but without the need to reinstall the model into the view (just the data would be refetched). The proper solution is to emit layoutChanged() or dataChanged() from the model.