Results 1 to 6 of 6

Thread: QTreeWidgetItem ?

  1. #1
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QTreeWidgetItem ?

    This is probably an easy question (except for me ), but how do you get a QTreeWidgetItem's parents column number? I have QTreeWidget that when a user selects an item, I am building a concatenated string of all of that selected item's parent's names. I can get the original item's name that is selected from currentColumn().
    Qt Code:
    1. ...
    2. foreach(item, selected)
    3. {
    4. attrName = item->text(mAttributeTreeWidget->currentColumn());
    5. if(item->parent())
    6. {
    7. QTreeWidgetItem * parent = item->parent();
    8. parentName = parent->text(??));
    9. if(parent->parent())
    10. {
    11. QTreeWidgetItem * plat = parent->parent();
    12. platName = plat->text(??);
    13. if(plat->parent())
    14. {
    15. QTreeWidgetItem * run = plat->parent();
    16. runName = run->text(??);
    17. }
    18. }
    19. }
    20. }
    21. QString fullName = runName + "::" + platName + "::" + attrName;
    22. ...
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidgetItem ?

    Quote Originally Posted by allensr View Post
    how do you get a QTreeWidgetItem's parents column number?
    But which one? Each QTreeWidgetItem consists of several columns --- it's something like a table row with several cells.

    Most likely currentColumn() will give you what you want.

  3. #3
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeWidgetItem ?

    But which one?
    I'm trying to get the parent of the QTreeWidgetItem selected by the user. So the user may select an item in column 2 but the parent is in column 0. I can get the selected item's column number, but still am not getting the parents'. You mentioned columnCount(), which I am using, but evidently doing it wrong. For the selected item I am making the call:

    Qt Code:
    1. mAttributeTreeWidget->currentColumn()
    To copy to clipboard, switch view to plain text mode 

    which is giving me the right column. But for the parent's column I am doing:

    Qt Code:
    1. QTreeWidgetItem * parent = item->parent();
    2. int column = parent()->treeWidget()->currentColumn());
    3. qDebug() << "Parent's Column " << column << endl;
    To copy to clipboard, switch view to plain text mode 

    But am getting the same column as the selected item. I know for a fact that the parent is in column 0, but this call always returns column 1.

    What am I doing wrong??

    TIA.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidgetItem ?

    Quote Originally Posted by allensr View Post
    So the user may select an item in column 2 but the parent is in column 0.
    In QTreeWidget an item represents the whole row, so there is no "item in column x", but "column x of item".

    Quote Originally Posted by allensr View Post
    But am getting the same column as the selected item. I know for a fact that the parent is in column 0, but this call always returns column 1.
    Each item has the same number of columns, so the parent has both column 0 and column 1. What data do you hold in those columns?

  5. #5
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeWidgetItem ?

    OK. So perhaps I am going about this wrong. The reason for the original question is I am trying to get the text from the parent and in the API you have to specify the column. I am not sure how to do that. Do you have a suggestion on the correct way to get this information?

    BTW, if I just hardcode 0 in the call I get the correct text string.

    Qt Code:
    1. foreach(item, selected)
    2. {
    3. attrName = item->text(mAttributeTreeWidget->currentColumn());
    4. if(item->parent())
    5. {
    6. QTreeWidgetItem * parent = item->parent();
    7. parentName = parent->text(??));
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidgetItem ?

    Quote Originally Posted by allensr View Post
    if I just hardcode 0 in the call I get the correct text string.
    If you want the text from column 0, there's no other way than hardcode that 0 somewhere.

    You can subclass QTreeWidgetItem and add a method that returns the text you want:
    Qt Code:
    1. QString MyTreeWidgetItem::name() const
    2. {
    3. return text( 0 );
    4. }
    5. ...
    6. MyTreeWidgetItem *item = dynamic_cast< MyTreeWidgetItem * >( widgetItem );
    7. if( item ) {
    8. parentName = item->name();
    9. }
    To copy to clipboard, switch view to plain text mode 
    Alternatively you can define an enum and name those columns:
    Qt Code:
    1. enum SomeItemColumn {
    2. Name = 0,
    3. SomethingElse = 1,
    4. AndSoOn = 2
    5. };
    6. ...
    7. parentName = item->text( Name );
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to jacek for this useful post:

    allensr (3rd January 2007)

Similar Threads

  1. QTreeWidgetItem swap or move up one level problem
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 24th September 2006, 18:34
  2. From extends QTreeWidgetItem emit signal?
    By patrik08 in forum Qt Programming
    Replies: 4
    Last Post: 19th May 2006, 14:54
  3. Model-Views: How to obtain QTreeWidgetItem from QModelIndex
    By johnny_sparx in forum Qt Programming
    Replies: 3
    Last Post: 8th April 2006, 20:37
  4. QTreeWidgetItem
    By Sarma in forum Qt Programming
    Replies: 1
    Last Post: 6th April 2006, 19:52
  5. enabling a QTreeWidgetItem as false
    By Kapil in forum Newbie
    Replies: 2
    Last Post: 3rd March 2006, 07:32

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.