Results 1 to 14 of 14

Thread: Get Row of specific QTreeWidgetItem in my QWidgetTree

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Get Row of specific QTreeWidgetItem in my QWidgetTree

    Quote Originally Posted by PeterSilie View Post
    But as i said it still does not consider the current sorting in the qtreewidget
    Yes it does work, trying running the code as is.

    Changing the sorting order automatically changes the row number.

    Screen Shot 2017-03-27 at 4.58.36 PM.png
    Screen Shot 2017-03-27 at 4.58.54 PM.png
    Screen Shot 2017-03-27 at 4.59.02 PM.png
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  2. #2
    Join Date
    Mar 2017
    Posts
    9
    Thanks
    3

    Default Re: Get Row of specific QTreeWidgetItem in my QWidgetTree

    Quote Originally Posted by Santosh Reddy View Post
    Yes it does work, trying running the code as is.

    Changing the sorting order automatically changes the row number.

    Screen Shot 2017-03-27 at 4.58.36 PM.png
    Screen Shot 2017-03-27 at 4.58.54 PM.png
    Screen Shot 2017-03-27 at 4.59.02 PM.png
    Ok it is working now, had to clean and rebuild my project in order to make it work.

    Thx alot

  3. #3
    Join Date
    Mar 2017
    Posts
    9
    Thanks
    3

    Default Re: Get Row of specific QTreeWidgetItem in my QWidgetTree

    Just in case somebody search for the same stuff again
    here is my solution:

    Qt Code:
    1. int rowsBelowIndex(const QModelIndex & index) {
    2. int count = 0;
    3. const QAbstractItemModel* model = index.model();
    4. int rowCount = model->rowCount(index);
    5. count += rowCount;
    6. for (int r = 0; r < rowCount; ++r)
    7. count += rowsBelowIndex(model->index(r, 0, index));
    8. return count;
    9.  
    10. }
    11.  
    12. int calculateRow(const QModelIndex & index) {
    13. int count = 0;
    14. if (index.isValid()) {
    15. count = (index.row()) + calculateRow(index.parent());
    16.  
    17. const QModelIndex parent = index.parent();
    18. if (parent.isValid()) {
    19. ++count;
    20. for (int r = 0; r < index.row(); ++r)
    21. count += rowsBelowIndex(parent.child(r, 0));
    22.  
    23. }
    24. }
    25.  
    26. return count;
    27. }
    28.  
    29.  
    30. int indexOfItem(QTreeWidgetItem* item) {
    31. QModelIndex index = indexFromItem(item);
    32. return calculateRow(index);
    33. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 11
    Last Post: 3rd March 2012, 22:21
  2. Replies: 5
    Last Post: 14th February 2011, 14:06
  3. QTreeWidgetItem
    By bismitapadhy in forum Qt Programming
    Replies: 12
    Last Post: 7th January 2011, 10:48
  4. Replies: 5
    Last Post: 23rd September 2010, 13:58
  5. QTreeWidgetItem ?
    By allensr in forum Qt Programming
    Replies: 5
    Last Post: 3rd January 2007, 17:51

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.