Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to tagging QTreewidget items?

How to tagging QTreewidget items?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 649 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    flote21
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by Bonnie
        #3

        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
        
        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved