Results 1 to 6 of 6

Thread: Determining QTreeWidget parent

  1. #1
    Join Date
    May 2008
    Posts
    25
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Determining QTreeWidget parent

    Hello all,

    I'm looking for a little help.

    I have a QTreeWidget with a number of parents and numerous children, i.e.

    parent1
    -child
    -child
    - child
    - child
    parent2
    -child
    - child
    parent3
    -child

    I'm using the contextMenuEvent to trigger a slot dependent upon the child picked. As such, I'm trying to determine which parent the child is associated with.

    How can I cast the parent of the child even if it is a child of a child?

    Cheers
    Robbie

  2. #2
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Determining QTreeWidget parent

    QTreeWidgetItem::parent() always works to get the parent of an Item. You have to be careful, though. On the top level this method returns null, not the root Item.
    Last edited by wysota; 30th March 2011 at 11:45.

  3. #3
    Join Date
    May 2008
    Posts
    25
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Determining QTreeWidget parent

    Have done a work around.

    item is the QTreeWidgetItem selected

    Qt Code:
    1. QTreeWidgetItem *upperParent = item;
    2. while(upperParent->parent())
    3. upperParent = upperParent->parent();
    4.  
    5. if(upperParent==TreeWidget->topLevelItem(0))
    6. ......
    7. else if (upperParent==TreeWidget->topLevelItem(1))
    To copy to clipboard, switch view to plain text mode 

    This lets me get back up to the parent of any item selected and attach different outcomes dependent on the child choice

    Cheers

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Determining QTreeWidget parent

    Nice spaghetti code, however you can do this:
    Qt Code:
    1. QTreeWidgetItem *item = ...;
    2. while(item->parent()) item = item->parent();
    3. int index = TreeWidget->invisibleRootItem()->indexOf(item);
    4. switch(index) {
    5. case 0: ...; break;
    6. case 1: ...; break;
    7. // etc.
    8. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    iaguirre (6th November 2012)

  6. #5
    Join Date
    May 2008
    Posts
    25
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Determining QTreeWidget parent

    Thanks - that seems a lot more elegant

    R

  7. #6
    Join Date
    Apr 2013
    Location
    North Carolina
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Determining QTreeWidget parent

    Here's some code I use to expand the tree all the way to the root.

    It's a recursive function that accepts a QTreeWidgetItem *item pointer and travels up the tree, starting as position of *item, and expands all parents in the *item's branch until it reaches the *item's topLevelItem.

    Qt Code:
    1. void MyWidget::expandTree(QTreeWidgetItem *item) {
    2. QTreeWidgetItem *parent = item->parent();
    3. if (parent) {
    4. parent->setExpanded(true);
    5. int topLevel = table->indexOfTopLevelItem(parent);
    6. if (topLevel!=-1) {
    7. QTreeWidgetItem *root = table->topLevelItem(topLevel);
    8. root->setExpanded(true);
    9. } else {
    10. if (item->parent()) {
    11. expandTree(item->parent());
    12. }
    13. }
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Determining QTextBlock font
    By smhall316 in forum Newbie
    Replies: 1
    Last Post: 19th October 2010, 22:00
  2. Replies: 5
    Last Post: 21st April 2010, 21:36
  3. Determining no. of CPU cores available
    By cnbp173 in forum Qt Programming
    Replies: 3
    Last Post: 21st April 2009, 17:55
  4. determining how to bounce the ball
    By Binji in forum Qt Programming
    Replies: 9
    Last Post: 29th December 2007, 12:24
  5. Determining Logged In State
    By TheGrimace in forum Qt Programming
    Replies: 2
    Last Post: 22nd October 2007, 15:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.