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. [Solved] Qt - How to get parent of QTreeWidgetItem
Forum Updated to NodeBB v4.3 + New Features

[Solved] Qt - How to get parent of QTreeWidgetItem

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 22.8k 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.
  • N Offline
    N Offline
    needhelp_gh
    wrote on last edited by
    #1

    Hi,

    Does anyone know how to get parent of a QTreeWidgetItem child?

    Thanks!!


    http://abstrusegoose.com/432

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      What's wrong with the existing QTreeWidgetItem::parent() method?

      1 Reply Last reply
      0
      • N Offline
        N Offline
        needhelp_gh
        wrote on last edited by
        #3

        Yes...I suppose I worded my question incorrectly, but thank you.

        I meant how can I return the string of the parent in C++ as that method returns a QTreeWidgetItem.


        http://abstrusegoose.com/432

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DerManu
          wrote on last edited by
          #4

          treeWidgetItem->parent()->text() ?

          1 Reply Last reply
          0
          • N Offline
            N Offline
            needhelp_gh
            wrote on last edited by
            #5

            :) Thanks, DerManu again!

            I tried:

            treeWidgetItem->parent()->text(column)

            Works!!


            http://abstrusegoose.com/432

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              When dereferencing a pointer, it is wise to check that the pointer is valid first. In your case, the result of parent() may be 0, and then the ->text(column) call would cause a crash.

              1 Reply Last reply
              0
              • N Offline
                N Offline
                needhelp_gh
                wrote on last edited by
                #7

                Thank you, Andre. I also noticed that it would crash if the item was the uppermost item & had no parent.

                I added this check to my c++ code (might be useful for anyone who comes across this in the future):

                @    QString cur = item->text(column);
                int root = ui->treeWidget->indexOfTopLevelItem(item);
                
                if ( root.parent() != None ) { // not top most
                    QString parent = item->parent()->text(column);
                }@
                

                http://abstrusegoose.com/432

                1 Reply Last reply
                1
                • D Offline
                  D Offline
                  DerManu
                  wrote on last edited by
                  #8

                  Are you sure this case isn't covered by what Andre said? I would expect parent() to simply return 0 on top level items (but I haven't checked that).

                  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