PDA

View Full Version : how to update a tree view model



chow
26th March 2009, 06:50
I have a question about how to update a tree view model when data changes. I will try to explain my problem by looking at the simpletreemodel example shipped with qt4.Lets assume I extend the example with a button, which will add some tree item (TreeItem instance) to the tree. Now, how can I update the visable QAbstractItemModel (TreeModel class) with these new changes.

spirit
26th March 2009, 07:00
a model and a view will be updated automatically. when data in model have been changed, model notify a view that the data have been changed and then view updates itself.

talk2amulya
26th March 2009, 07:01
you should have a look at

http://doc.trolltech.com/4.3/model-view-creating-models.html#an-editable-model

or better look at the whole of it:

http://doc.trolltech.com/4.3/model-view-programming.html

chow
26th March 2009, 07:23
a model and a view will be updated automatically. when data in model have been changed, model notify a view that the data have been changed and then view updates itself.

Thanks,

But the treeModel is my own model which refer to the simpletreemodel example
shipped with qt4. It can't be updated automatically, when data in model have been changed.

Regards.

chow
26th March 2009, 07:24
you should have a look at

http://doc.trolltech.com/4.3/model-view-creating-models.html#an-editable-model

or better look at the whole of it:

http://doc.trolltech.com/4.3/model-view-programming.html

Thanks, but it can't help.

spirit
26th March 2009, 07:27
so, you use own model, right? so, when you change data you need to emit signal QAbstractItemModel::dataChanged.

you have to listen talk2amulya and read last link.

talk2amulya
26th March 2009, 07:46
if you read those links, your problem would be solved undoubtedly..also, u say


It can't be updated automatically, when data in model have been changed.

u say data in model is changed then what is not updated automatically? its simple:

make your item editable
use setData(..)
change ur model/source of model
emit dataChanged()

chow
26th March 2009, 14:53
if you read those links, your problem would be solved undoubtedly..also, u say



u say data in model is changed then what is not updated automatically? its simple:

make your item editable
use setData(..)
change ur model/source of model
emit dataChanged()



But the problem is ..

Lets assume I extend the example with a button, which will add some tree
item (TreeItem instance) to the tree. Now, how can I update the
visable QAbstractItemModel (TreeModel class) with these new changes.
One way I found, I think, is to call the reset() member function of
QAbstractItemModel, but this is a bit drastic, because it will parse the
whole tree again.

An other solution, I assume, is to emit a dataChanged() signal or a
layoutChaged() signal. The problem I have with these signals is that
a have no clue on how to create the arguments (QModelIndex) in case
of a tree view.

An other problem is that the layoutChanged() signal does not seems to be
documented in Qt assistant.

aamer4yu
26th March 2009, 15:27
Lets assume I extend the example with a button, which will add some tree
item (TreeItem instance) to the tree

Dont assume... get started and try an example... I guess the Qt Demos will be sufficient for the problems u face.. see how they add data in the model...
still if you cant make it work, then post the prob u get,,, but get started first :)