Results 1 to 6 of 6

Thread: Determining QTreeWidget parent

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.


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

    iaguirre (6th November 2012)

  3. #2
    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

  4. #3
    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.