PDA

View Full Version : How to know wich item is clicked QTreeWidget



^NyAw^
6th November 2007, 17:07
Hi,

I have a QTreeWidget that I insert some items at column 0. This items have other items,...

What I want is to know wich item is pressed. I'm reading about QModelIndex but for a tree similar of this:
- Item1
-SubItem1
-SubSubItem1

The row and column of these three items are always (0,0).
I have a SLOT called every time an item is clicked and I recive the clicked item there.

Any idea how to do it?

Thanks,

EricF
6th November 2007, 17:22
Check model index's parent's ModelIndex.

^NyAw^
6th November 2007, 17:26
Hi,

Could you be more explicit?

Thanks,

EricF
6th November 2007, 17:29
Yeah sorry I was searching for a better answer. Here's a link:

http://doc.trolltech.com/4.3/model-view-model.html#parents-of-items

I guess you're receiving a model index in your slot.

You can recursively call parent() on model index to retrieve the higher level model index to get the full path of the selection.

jpn
6th November 2007, 17:34
With QTreeWidget there shouldn't be need to use QModelIndexes, at least for that. :) QTreeWidget offers a signal QTreeWidget::itemClicked(QTreeWidgetItem* item, int column). What do you want to actually do when a certain item is clicked?

^NyAw^
6th November 2007, 17:34
Hi,

Ok, will try to use it.

Thanks,

^NyAw^
6th November 2007, 18:35
Hi,

I'm not getting it work yet.

I have the tree populated like the attached image.

Thanks,

marcel
6th November 2007, 18:37
But why don't you use what jpn suggested? It is as clear as that, even simpler that using QModelIndex.

^NyAw^
6th November 2007, 18:45
Hi,


But why don't you use what jpn suggested? It is as clear as that, even simpler that using QModelIndex.

Sorry, but I didn't see jpn reply (I supose I was writting too).


What do you want to actually do when a certain item is clicked?

What I want is to know wich item is clicked.
I have an internal structure with some LinkedLists. When I add, remove, rename, ... an item, it represents one object of the internal data, so I have to know wich object is represented by the item that is selected.

Thanks,

jpn
6th November 2007, 18:53
I have an internal structure with some LinkedLists. When I add, remove, rename, ... an item, it represents one object of the internal data, so I have to know wich object is represented by the item that is selected.
Then you should seriously consider saying goodbye to QTreeWidget and switching to model based approach. You should wrap the internal structure into QAbstractItemModel instead of replicating it to another structure.

jpn
6th November 2007, 19:12
By the way, does the internal structure of yours have any kind of identifier numbers or something like that? Or how did you plan to match corresponding items anyway? One possibility for solving this in a quick and dirty way is to abuse QTreeWidgetItem::setData() with Qt::UserRole (http://doc.trolltech.com/latest/qt.html#ItemDataRole-enum) to identify which item corresponds which part of the internal structure.

^NyAw^
6th November 2007, 20:54
Hi,


Then you should seriously consider saying goodbye to QTreeWidget and switching to model based approach. You should wrap the internal structure into QAbstractItemModel instead of replicating it to another structure.

Wrap the internal structure to a model is to get a model and my structure in parallel?



By the way, does the internal structure of yours have any kind of identifier numbers or something like that?


Yes, I have LinkedLists that have internal nodes with uniques ID for each node.

Really I don't know wich is the best solution because my internal structure is handcoded (I had coded linkedLists, nodes, ... manually because it was done before I discovered Qt).

Thanks,

jpn
6th November 2007, 21:16
Wrap the internal structure to a model is to get a model and my structure in parallel?
QAbstractItemModel subclass would itself keep no data at all. It would be a plain interface for to your internal data. That's how Qt's view classes would be able to represent your internal data without need of copying the internal data to another structure, QTreeWidgetItems in your case.



Yes, I have LinkedLists that have internal nodes with uniques ID for each node.

Really I don't know wich is the best solution because my internal structure is handcoded (I had coded linkedLists, nodes, ... manually because it was done before I discovered Qt).
So basically you could mark each QTreeWidgetItem with corresponding identifier:


int id = ... // id of the corresponding item in the internal data structure
item->setData(0, Qt::UserRole, id);

And once you react to clicked-signal you could ask for the identifier:


int id = item->data(0, Qt::UserRole).toInt();
... // find the corresponding item in the internal data structure with help of id

Roles starting from Qt::UserRole and upwards can be used for application specific things. QTreeWidgetItem will be able to store them, but will of course use them for nothing by itself.

^NyAw^
7th November 2007, 00:47
Hi,

Hey jpn! It's so easy now!

I just tell the item wich kind of item is, and when one is clicked, if it represents a node into a linkedList, I can tell the item parent wich position the item stays.

Thanks for all that have replied and helped me, maybe I will ask some other questions,