Results 1 to 5 of 5

Thread: trying to access treewidget indexFromItem

  1. #1
    Join Date
    May 2012
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question trying to access treewidget indexFromItem

    Hello all,

    I have been trying to make use of the QTreeWidget's indexFromItem to make use to access an actual index on a QStringList (for file writing purposes).

    I can't show code b/c this is an offsite computer, but I need help.

    when I try to do something like this:

    Qt Code:
    1. void on_treeWidget_itemChanged(QTreeItem *item, int column)
    2. {
    3.  
    4. int Index = ui->treeWidget->indexFromItem(item->parent(),0).row();
    5. }
    To copy to clipboard, switch view to plain text mode 

    i get this error message saying indexFromItem is a protected member.

    Ive tried direct input, such as:

    Qt Code:
    1.  
    2. list.removeAt(ui->treeWidget->indexFromItem(item->parent(),0).row());
    To copy to clipboard, switch view to plain text mode 

    or even a failed attempt at getting and setting

    I'm pressed for time, but I was hoping that somebody had the answers to this dilemma.

    Thanks in advance,
    gamenovice

    ps. If I am not being clear, let me know

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: trying to access treewidget indexFromItem

    This is a C++ understanding issue. Protected member functions are only available to the class that contains them or classes that inherit from that class. They behave as private to all others attempting access.

    You are trying to determine which child of the changed item's grandparent is the changed item's parent. Something like:
    Qt Code:
    1. int index = -1; // means not found
    2. QTreeWidgetItem *parent = item->parent();
    3. if (parent) {
    4. QTreeWidgetItem *grandparent = parent->parent();
    5. if (grandparent)
    6. index = grandparent->indexOfChild(parent);
    7. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2012
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: trying to access treewidget indexFromItem

    this does work, thanks,

    but i seem to have noticed another problem:

    if i were to have 2 item entries in my tree widget:


    Qt Code:
    1. bob
    2. id
    3. full name
    4. last name
    5. email
    6. phone
    7.  
    8. sally
    9. id
    10. full name
    11. last name
    12. email
    13. phone
    To copy to clipboard, switch view to plain text mode 

    now if i were to edit sally phone number in the treewidget, then the next time i open up the widget, i see that bobs phone number and sally's phone number have swapped around.



    Qt Code:
    1. bob
    2. id
    3. full name
    4. last name
    5. email
    6. sally's phone
    7.  
    8. sally
    9. id
    10. full name
    11. last name
    12. email
    13. bob's phone
    To copy to clipboard, switch view to plain text mode 

    I know that I was trying to get the index of the parent for the widget, but is there any way to make the ordering and index of the parent itself consistent, so that this doesn't happen?

    thanks,
    gamenovice


    Added after 6 minutes:


    another wierd problem that show's up is that i cannot seem to set the text of the parent. if i try qt crashes. so i have to implement my widget like this:

    Qt Code:
    1. *blank*
    2. id
    3. name
    4. etc.
    To copy to clipboard, switch view to plain text mode 

    any ideas what's going on there?
    Last edited by gamenovice; 6th May 2012 at 16:47.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: trying to access treewidget indexFromItem

    The QTreeWidget does not reorder items in the tree magically. This must be something you are doing in your code's logic. Since we cannot see your code you will have to find this yourself.

    You cannot set the text of the parent item if there is no parent. Check for parent() returning null (which you will notice I was doing in the example I gave you).

  5. #5
    Join Date
    May 2012
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: trying to access treewidget indexFromItem

    thanks for the advice,

    I actually found a pretty obvious solution.

    Make a grandfather node like specified when looking for the index(earlier in thread) and restructure the tree accordingly. It seems much more manageable that way as well.

Similar Threads

  1. add items to treewidget
    By Shien in forum Qt Programming
    Replies: 15
    Last Post: 1st January 2011, 19:40
  2. Replies: 2
    Last Post: 18th May 2010, 22:44
  3. How to add subitem in treewidget
    By phillip_Qt in forum Qt Programming
    Replies: 11
    Last Post: 16th November 2009, 11:22
  4. How to set the sizehint for treeWidget
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 12th June 2007, 11:35
  5. Painting TreeWidget
    By VireX in forum Qt Programming
    Replies: 9
    Last Post: 9th May 2007, 08:36

Tags for this Thread

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.