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. row of item in QTreeWidget
Forum Updated to NodeBB v4.3 + New Features

row of item in QTreeWidget

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 7.6k Views 3 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.
  • HarbH Offline
    HarbH Offline
    Harb
    wrote on last edited by
    #1

    Hi!

    I have QTreeWidget, it is 2 level Tree (top level items, and their children). I click on childern item, and want to obtain its row index. For example I have 1 top level item, and 2 childern, I click on the second childern and want to get int=1. It should be quite easy, but I am a little bit confused yet with that model/view stuff. So could you please help?

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

      Hi
      TreeWidget is itembased and it seems that
      currentindex().row() is not really working
      as one would expect. (at least not for me. always zero)

      I ended up using treeWidget->currentItem() which gives the item.
      Maybe you can use that too?

      HarbH 1 Reply Last reply
      1
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        If you connect to the clicked() signal of the QTreeWidget you can use the index parameter to get what you want, e.g.

        connect(treeWidget, &QTreeWidget::clicked, [](const QModelIndex &index) {
             qDebug() << index.row();
        });
        
        1 Reply Last reply
        1
        • mrjjM mrjj

          Hi
          TreeWidget is itembased and it seems that
          currentindex().row() is not really working
          as one would expect. (at least not for me. always zero)

          I ended up using treeWidget->currentItem() which gives the item.
          Maybe you can use that too?

          HarbH Offline
          HarbH Offline
          Harb
          wrote on last edited by
          #4

          @mrjj Well, tnx for your answer, currentIndex().row() works! I don't know why it didn't work in your case. But still it looks a little bit ugly, i don't undertand why i can't get index from QTreeWidgetItem directly. There is QTreeWidget.IndexFromItem(QTreeWidgetItem* ) method, but it is protected!

          mrjjM 1 Reply Last reply
          0
          • HarbH Harb

            @mrjj Well, tnx for your answer, currentIndex().row() works! I don't know why it didn't work in your case. But still it looks a little bit ugly, i don't undertand why i can't get index from QTreeWidgetItem directly. There is QTreeWidget.IndexFromItem(QTreeWidgetItem* ) method, but it is protected!

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

            @Harb
            It did ? Nice.
            That is good to know.
            Must have done something wrong with mine.

            When I fooled around with TreeWidget it seems to be that - while its item based, it
            still uses a tree model internal and hence it does not have a flat index but uses the
            QModelIndex Class index feature.

            Can I ask what you need the index for ?

            HarbH 1 Reply Last reply
            0
            • mrjjM mrjj

              @Harb
              It did ? Nice.
              That is good to know.
              Must have done something wrong with mine.

              When I fooled around with TreeWidget it seems to be that - while its item based, it
              still uses a tree model internal and hence it does not have a flat index but uses the
              QModelIndex Class index feature.

              Can I ask what you need the index for ?

              HarbH Offline
              HarbH Offline
              Harb
              wrote on last edited by
              #6

              @mrjj Well, I need indexes to identify what part of a file I want ot load. My Top Level items in my QTreeWidget are names of files, and child items are "items" in the files, basically for each "item" relate big ammount of data into the file. I don't want to load whole file into the memory because it can be very large(40-50 Mb), so I just give user an interfces, so he can load just a part of a data, which he needs now.

              mrjjM 1 Reply Last reply
              0
              • HarbH Harb

                @mrjj Well, I need indexes to identify what part of a file I want ot load. My Top Level items in my QTreeWidget are names of files, and child items are "items" in the files, basically for each "item" relate big ammount of data into the file. I don't want to load whole file into the memory because it can be very large(40-50 Mb), so I just give user an interfces, so he can load just a part of a data, which he needs now.

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

                @Harb
                Ahh that makes sense.
                So index is a easy to use. No need for extra data i guess.

                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