PDA

View Full Version : QTreeWidget and Graphics scene



aamer4yu
21st December 2006, 11:48
I have a QTreeWidget which shows data about graphics items and top level items are like groups which have different graphics items... attaching a snapshot of the treewidget.

The tree displays info about items positions and names... I want to update the tree when items are moved (as items pos is displayed in the tree). How do I map the nodes of tree to the GraphicsItems ?? I mean how do i obtain a pointer to the GraphicsItem based on the node clicked in the tree ?? and how to refresh the data in the tree based on movements in the scene ??

One method I found in the search forums was to subclass QTreeWidgetItem and have the desired info in it...is it a good one ??

Please suggest...

wysota
21st December 2006, 12:00
The proper way would be to use your own model and keep a pointer to the graphics item in the model index. But if you want to stick with the treewidget approach, then you can make a QMap<QTreeWidgetItem*, QGraphicsItem*> map and hold the info there.

aamer4yu
21st December 2006, 12:28
The proper way would be to use your own model and keep a pointer to the graphics item in the model indexI tried to read about models, and right now its going over my head.
Also I need a tree with single column, so I am not sure to go in for a model for this... there must be a simple way

But if you want to stick with the treewidget approach, then you can make a QMap<QTreeWidgetItem*, QGraphicsItem*> map and hold the info there.

I cud have used this approach, but not all treeWidgetItems are related to GraphicsItem, like the PL.. it contains a list of graphicsitems which are contained in it.

Also in future I may need to add tree nodes based on certain lists and not graphicsitems..

and the subclassing QTreewidgetItem idea was given by you(wysota) in this thread (http://www.qtcentre.org/forum/f-newbie-4/t-qlist-question-4903.html/?highlight=qtreewidget)

what say ??:rolleyes:

Also I would be glad if u teach me something about the model , model/view architecture in context with this prpblem, I searched thru Qt Examples but cudnt find a suitable example related to my problem.. only if experts like u speak something, it wud be a great speed up for guys like me :D

wysota
21st December 2006, 13:03
I tried to read about models, and right now its going over my head.
It's better to switch to the model approach sooner than later, you'll have less rewriting to do :)

For the beginning you can cheat a little, use QStandardItemModel and either create a custom item role which will hold a pointer to a graphicsview item or reimplement the index() method of the model and store the pointer as the internal pointer. The latter solution is better, but it'll only work if QStandardItemModel doesn't use that internal field for itself.


Also I need a tree with single column, so I am not sure to go in for a model for this... there must be a simple way
The point is that you don't want to do simple things, so you shouldn't use simple solutions.


I cud have used this approach, but not all treeWidgetItems are related to GraphicsItem, like the PL.. it contains a list of graphicsitems which are contained in it.
Does it matter? You only store there what you like.


Also in future I may need to add tree nodes based on certain lists and not graphicsitems..
That's another argument for using a full featured model.


Also I would be glad if u teach me something about the model , model/view architecture in context with this prpblem, I searched thru Qt Examples but cudnt find a suitable example related to my problem.. only if experts like u speak something, it wud be a great speed up for guys like me :D

When I find some tme (during Christmas maybe?) I'll post an example model made from scratch on my website (in the GL model article series), so you'll be able to take a look.

aamer4yu
21st December 2006, 13:13
Thanks for the reply...
Currently I am making a prototype of an application which I have to make later on.. and need to finish it soon....
so I guess implementing the model will take time and i wont be able to meet the deadlines :o in meantime I have to stick to some shortcuts ;)
I can explore optimising things later for the main project....


When I find some tme (during Christmas maybe?) I'll post an example model made from scratch on my website (in the GL model article series), so you'll be able to take a look.:cool: I will be waiting for it eagerly...
Thanx again

wysota
21st December 2006, 13:49
Currently I am making a prototype of an application which I have to make later on.. and need to finish it soon....
It takes not more than an hour to implement a simple model.

so I guess implementing the model will take time and i wont be able to meet the deadlines :o in meantime I have to stick to some shortcuts ;)
Sure, just make sure the shortcuts are really shorter than the full-featured solution ;)

aamer4yu
21st December 2006, 16:49
It takes not more than an hour to implement a simple model.
an hour for you ?? i wonder how long it will take for me.. :D

jokes apart,,, i will try if u say so... can u give some steps regarding the same..
and yeah, I will be waiting for ur example :)
Thx

wysota
21st December 2006, 17:24
an hour for you ?? i wonder how long it will take for me.. :D
You can start by modifying the simple tree model example. It'll take you 10 minutes then :)