Unsolved How to tagging QTreewidget items?
-
I am pretty new in python (and this is my first post here). so Hello everyone!
I developed a Treeview widget using Tkinter. I used the next code sentence to create the Treeview items and subitems with Tkinter:
Treeview.insert("", END, text=NAMESTRUCTURE + lst_ws[n_sheet], iid=lst_ws[n_sheet], tags=("mytag",)) Treeview.insert(profile_split[0], END, text=NAMEPROFILE + profile_split[1], iid=profile, tags=("mytag",))
Therefore, every item on Tkinter Treeview had its only specific "tag" and thanks to this method I was able to get coordinates of the item in the Treeview:
def treeItemSel(self, event): # get the tree selected item tree_coords = (self.tree_view.winfo_pointerx() - self.tree_view.winfo_rootx(), self.tree_view.winfo_pointery() - self.tree_view.winfo_rooty()) tree_item = self.tree_view.identify('item', *tree_coords)
Currently I am migrating the previous python code because I want to use the QT GUI. But I am wondering how to create a Treeview with tags and how to get the coordinates of the Treeview items when one of them is selected.
Note: To create a Treeview with PyQt I used Qt.QTreeWidgetItem but it doesn't create a specific tag for every item.
The reason because I would like to have a "tag" or some kind of identification is because I want to load different pictures according to the treeview item selected. And the name of the pic is different to the name of the treeview item. So I need to find a solution in Qt GUI to make a relation between the name of the picture and name of the Qtreeview item. Any idea?
Thanks in advance.
Greetings
-
Hi and welcome to devnet,
That's something you should rather store at the model level.
If you are using the QTreeWidget, you can create items by passing a string and an image for example.
-
The selected items can be got from
QList<QTreeWidgetItem *> QTreeWidget::selectedItems() const
And the rect of a visible item can be get from
QRect QTreeWidget::visualItemRect(const QTreeWidgetItem *item) const
But the rect is relative to its parent, not global. You will need to map it if you want global coordinate.
There is no "TAG" in QTreeWidgetItem, but like @SGaist said an item can have an image (as icon) and a string (as name) at the same time.
If you still need to store other information, you can set user defined data to it.void QTreeWidgetItem::setData(int column, int role, const QVariant &value)
You can define a number as role, but must equal or bigger than Qt::UserRole, and set any kind of data as value.
The value can be retrieved by:QVariant QTreeWidgetItem::data(int column, int role) const
If you set a QString as value, you can convert the retrieved QVariant by:
QString QVariant::toString() const