How to know wich item is clicked QTreeWidget
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,
Re: How to know wich item is clicked QTreeWidget
Check model index's parent's ModelIndex.
Re: How to know wich item is clicked QTreeWidget
Hi,
Could you be more explicit?
Thanks,
Re: How to know wich item is clicked QTreeWidget
Yeah sorry I was searching for a better answer. Here's a link:
http://doc.trolltech.com/4.3/model-v...rents-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.
Re: How to know wich item is clicked QTreeWidget
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?
Re: How to know wich item is clicked QTreeWidget
Hi,
Ok, will try to use it.
Thanks,
1 Attachment(s)
Re: How to know wich item is clicked QTreeWidget
Hi,
I'm not getting it work yet.
I have the tree populated like the attached image.
Thanks,
Re: How to know wich item is clicked QTreeWidget
But why don't you use what jpn suggested? It is as clear as that, even simpler that using QModelIndex.
Re: How to know wich item is clicked QTreeWidget
Hi,
Quote:
Originally Posted by
marcel
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).
Quote:
Originally Posted by
jpn
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,
Re: How to know wich item is clicked QTreeWidget
Quote:
Originally Posted by
^NyAw^
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.
Re: How to know wich item is clicked QTreeWidget
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 to identify which item corresponds which part of the internal structure.
Re: How to know wich item is clicked QTreeWidget
Hi,
Quote:
Originally Posted by
jpn
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?
Quote:
Originally Posted by
jpn
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,
Re: How to know wich item is clicked QTreeWidget
Quote:
Originally Posted by
^NyAw^
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.
Quote:
Originally Posted by
^NyAw^
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:
Code:
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:
Code:
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.
Re: How to know wich item is clicked QTreeWidget
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,