PDA

View Full Version : class MyReadOnlyTreeModel : public QAbstractItemModel



Yong
20th April 2008, 17:30
I need a fully working sample of
class MyReadOnlyTreeModel : public QAbstractItemModel

where the content is changed at run time by a worker thread.

thanks.

wysota
20th April 2008, 18:30
Hmm... "ReadOnlyModel" that is changed? :D How about a small green sphere that looks like a big red cube? ;)

If you provide more details of what you want and tell us what you have already tried, then maybe we'll be able to help.

Yong
20th April 2008, 18:36
Hmm... "ReadOnlyModel" that is changed? :D How about a small green sphere that looks like a big red cube? ;)

If you provide more details of what you want and tell us what you have already tried, then maybe we'll be able to help.


Readonly meaning it does not implement setData() to allow the views to change data.
But some threads can change the internal strucutre that is represented by the model, and
this will lead the view to update correspondingly.

Internal structure is something like:

struct TheType
{
...
TheType* parent;
List<TheType*> _chidren;
};

Does this sound reasonable or I'm missing something ?
Or there is another approach for this ?

wysota
20th April 2008, 20:43
You'll need some synchronization mechanism if you want to use multiple threads when working on the model like that. But I'd suggest using signals and slots for updating the model.

Take a look here: http://blog.wysota.eu.org/index.php/2006/12/26/remote-models/

It deals with a similar problem (updating the model on the fly).

Yong
20th April 2008, 21:22
Sorry, I think I have described the problem wrongly again. The problem is not in thread
synchronization. Let's assume there is one thread only.

You can take a look at the code attached, and here is what I would like to understand:

1) After add-new-tree-item or update-an-existing-item in the internal tree, what to emit for
the corresponding item to let the views know that info about that item has changed.

2) I have tried many implementations for the model, and hardly can get it right.
I suspect something is wrong there.

3) The initial idea was to have a read-only model so that view can't modify it.
but samples I found on read-only are populating data in the beginning only, not run time.

4) any blaming to what I have posted, are welcome

thanks

Yong
20th April 2008, 21:30
oh, just saw the examples link there. my bad.