Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Adding a property to QTreeWidgetItem
Forum Updated to NodeBB v4.3 + New Features

Adding a property to QTreeWidgetItem

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 3 Posters 915 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.
  • V Offline
    V Offline
    vikke
    wrote on last edited by
    #1

    Is is possible to add a property, I know that's not possible, but something similar at least to it to a QTreeWidgetItem. I want to be able to add a setting for each row, so I know how to display it.
    So I have this QTreeWidget which has a bunch of stuff in it and I want to make it display a column in either hex, decimal or octal depending on the choice from a QActionGroup when you right click it. My current solution would be to add a suffix to it but it would be nicer to have a property or something.

    JonBJ 1 Reply Last reply
    0
    • V vikke

      Is is possible to add a property, I know that's not possible, but something similar at least to it to a QTreeWidgetItem. I want to be able to add a setting for each row, so I know how to display it.
      So I have this QTreeWidget which has a bunch of stuff in it and I want to make it display a column in either hex, decimal or octal depending on the choice from a QActionGroup when you right click it. My current solution would be to add a suffix to it but it would be nicer to have a property or something.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @vikke
      You add whatever "properties" you want by sub-classing QTreeWidgetItem and putting whatever member variables you want into that. Then you add created instances of your sub-class into the QTreeWidget. This principle applies in many places in Qt.

      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        Just as a note.
        There is also UserRoles for such data.
        QTreeWidgetItem * item = new QTreeWidgetItem();
        item->setData(0,Qt::UserRole, true );
        item->setData(0,Qt::UserRole+1, 100 );

        V 1 Reply Last reply
        3
        • mrjjM mrjj

          Hi
          Just as a note.
          There is also UserRoles for such data.
          QTreeWidgetItem * item = new QTreeWidgetItem();
          item->setData(0,Qt::UserRole, true );
          item->setData(0,Qt::UserRole+1, 100 );

          V Offline
          V Offline
          vikke
          wrote on last edited by
          #4

          @mrjj thank you very much, that solved my problem, I made an enum for the role so now I can add new ones as needed, is there some good way to make them or is it just making your own custom enum

          mrjjM 1 Reply Last reply
          0
          • V vikke

            @mrjj thank you very much, that solved my problem, I made an enum for the role so now I can add new ones as needed, is there some good way to make them or is it just making your own custom enum

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @vikke
            Hi
            enums are fine as the only important thing is that they all have unique values
            and using an enum makes that easy.
            It also makes code easier to read as you can better guess what data is.

            1 Reply Last reply
            0

            • Login

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