Results 1 to 14 of 14

Thread: Get Row of specific QTreeWidgetItem in my QWidgetTree

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

    Default Get Row of specific QTreeWidgetItem in my QWidgetTree

    So i just started working with Qt and now i have problem,

    i would like to get the row in which a specific QTreeWidgetItem (not selected!) is in my QWidgetTree.

    I didn't find a real solution for this issue until now, so maybe an experienced QT Developer knows a good way to get this information.

    Thx in advance.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 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

    Qt Code:
    1. int QTreeWidget::indexOfTopLevelItem(QTreeWidgetItem *item) const
    To copy to clipboard, switch view to plain text mode 
    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.

  3. #3
    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
    Qt Code:
    1. int QTreeWidget::indexOfTopLevelItem(QTreeWidgetItem *item) const
    To copy to clipboard, switch view to plain text mode 
    does not work for me, always returns -1 (except for my root item), maybe because i want to get the index of an item which is in a subtree of my qtreewidget

    root
    --sub1
    ----sub1.1
    ----sub1.2 <-- get the index for this, shoud be 3
    --sub2
    --sub3

    i tryed this but i didnt work because the children are not in the same order as my view (e.g. root.children = [sub2, sub1, sub3]) :

    Qt Code:
    1. int indexOfItem(QTreeWidgetItem* item){
    2. int index = 0;
    3. parent = item->parent();
    4. if(parent != 0){
    5. index += parent->indexOfChild(item)+1 + indexOfItem(parent);
    6. }
    7.  
    8. return index;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by PeterSilie; 23rd March 2017 at 16:57.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 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
    root
    --sub1
    ----sub1.1
    ----sub1.2 <-- get the index for this, shoud be 3
    --sub2
    --sub3
    [/CODE]
    Please explain why 3 ?
    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.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get Row of specific QTreeWidgetItem in my QWidgetTree

    Please explain why 3 ?
    If you are confused and think that a tree uses the same kind of row indexing as a table, then root = 0, sub1 = 1, sub1.1 = 2, and sub1.2 = 3. But a tree does not use the same indexing as a table, as the OP is discovering.

    In a tree, the row index is numbered with respect to the item's parent, so in this case sub1.2 has row index = 1. Item sub1.1 has row index 0. Item sub1 also has row index = 0, but that is numbered with respect to the root item.
    Last edited by d_stranz; 24th March 2017 at 17:11.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    PeterSilie (28th March 2017)

  7. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 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 d_stranz View Post
    If you are confused...
    No I am not, just trying to understand why OP thinks so.
    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.

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

    Default Re: Get Row of specific QTreeWidgetItem in my QWidgetTree

    Quote Originally Posted by d_stranz View Post
    If you are confused and think that a tree uses the same kind of row indexing as a table, then root = 0, sub1 = 1, sub1.1 = 2, and sub1.2 = 3. But a tree does not use the same indexing as a table, as the OP is discovering.

    In a tree, the row index is numbered with respect to the item's parent, so in this case sub1.2 has row index = 1. Item sub1.1 has row index 0. Item sub1 also has row index = 0, but that is numbered with respect to the root item.
    Yes this is exactly what i like to get. Actually it is more the row number than the index
    The code snippet i posted a bit above will do this, but i need some functionality to order the children the same way they are ordered as they are displayed. tryed QTreeWidgetItem.sortChildren() but it does not work since the item is associated with my QTreeWidget

    Quote Originally Posted by PeterSilie View Post
    Qt Code:
    1. int indexOfItem(QTreeWidgetItem* item){
    2. int index = 0;
    3. parent = item->parent();
    4. if(parent != 0){
    5. index += parent->indexOfChild(item)+1 + indexOfItem(parent);
    6. }
    7.  
    8. return index;
    9. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by Santosh Reddy View Post
    No I am not, just trying to understand why OP thinks so.
    I guess he means that i am confused and not you



    So actually i search for a functionality to get the row number of a specific item in my treewidget but with respect to the ordering in my current view.
    Last edited by PeterSilie; 27th March 2017 at 07:55.

  9. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 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

    Here is an example, see if this is what you want. Click the item and it's row number will appear above.

    Qt Code:
    1. #include <QtWidgets>
    2. // TreeWidget.h
    3. class TreeWidget : public QTreeWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit TreeWidget(QWidget * parent = 0)
    8. : QTreeWidget(parent)
    9. {
    10. setColumnCount(1);
    11. AddItems();
    12. setSortingEnabled(true);
    13. connect(this, SIGNAL(clicked(QModelIndex)), SLOT(evalRow(QModelIndex)));
    14. }
    15.  
    16. signals:
    17. void selectedRowChanged(const QString & row);
    18.  
    19. private slots:
    20. void evalRow(const QModelIndex & index)
    21. {
    22. QString row = "<not selected>";
    23. QAbstractItemModel * m = model();
    24.  
    25. if(m)
    26. row = QString::number(countRow(index));
    27.  
    28. emit selectedRowChanged(row);
    29. }
    30.  
    31. private:
    32. int countRow(const QModelIndex & index)
    33. {
    34. int count = 0;
    35.  
    36. if(index.isValid())
    37. {
    38. count = (index.row() + 1) + countRow(index.parent());
    39.  
    40. const QModelIndex parent = index.parent();
    41. if(parent.isValid())
    42. {
    43. for(int r = 0; r < index.row(); ++r)
    44. count += model()->rowCount(parent.child(r, 0));
    45. }
    46. }
    47.  
    48. return count;
    49. }
    50.  
    51. void AddItems()
    52. {
    53. QTreeWidgetItem * root = new QTreeWidgetItem(QStringList() << "root");
    54. QTreeWidgetItem * sub_1 = new QTreeWidgetItem(QStringList() << "sub_1");
    55. QTreeWidgetItem * sub_1_1 = new QTreeWidgetItem(QStringList() << "sub_1_1");
    56. QTreeWidgetItem * sub_1_2 = new QTreeWidgetItem(QStringList() << "sub_1_2");
    57. QTreeWidgetItem * sub_2 = new QTreeWidgetItem(QStringList() << "sub2");
    58. QTreeWidgetItem * sub_3 = new QTreeWidgetItem(QStringList() << "sub3");
    59.  
    60. root->addChild(sub_1);
    61. root->addChild(sub_2);
    62. root->addChild(sub_3);
    63.  
    64. sub_1->addChild(sub_1_1);
    65. sub_1->addChild(sub_1_2);
    66.  
    67. addTopLevelItem(root);
    68. }
    69. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // main.cpp
    2. #include "TreeWidget.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QLabel * label = new QLabel;
    9. TreeWidget * treeWidget = new TreeWidget;
    10.  
    11. QWidget mainWidget;
    12.  
    13. QVBoxLayout * layout = new QVBoxLayout(&mainWidget);
    14. layout->addWidget(label);
    15. layout->addWidget(treeWidget);
    16.  
    17. QObject::connect(treeWidget, SIGNAL(selectedRowChanged(QString)), label, SLOT(setText(QString)));
    18.  
    19. mainWidget.show();
    20.  
    21. return app.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 
    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.

  10. The following user says thank you to Santosh Reddy for this useful post:

    PeterSilie (28th March 2017)

  11. #9
    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
    Here is an example, see if this is what you want. Click the item and it's row number will appear above.
    Well works kinda the same way as the solution i posted already, means that the same problem with the ordering of the items occurs again.

    Somehow the algorithm to calculate the row / index of the item must consider the ordering of the items displayed (different columns like label, path, etc. used to (re-)order the items in the treewidget) and neither my solution nor your solution does this

    But still thx alot for spending your time to help me out with this issue, hope that we will find a working solution for this somehow.
    Last edited by PeterSilie; 27th March 2017 at 10:21.

  12. #10
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 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
    Somehow the algorithm to calculate the row / index of the the item must consider the ordering of the items displayed and neither my solution nor your solution does this
    Are you sure, did you run the code I posted. It considers the new sorting order, just re-sort and click the item again. Refresh may be an issue....


    Anyway, you could connect to sort indicator and refresh the index row number, by storing the persistent index
    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. class TreeWidget : public QTreeWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit TreeWidget(QWidget * parent = 0)
    8. : QTreeWidget(parent)
    9. {
    10. setColumnCount(1);
    11. AddItems();
    12. setSortingEnabled(true);
    13. connect(this, SIGNAL(clicked(QModelIndex)), SLOT(evalRow(QModelIndex)));
    14. connect(this->header(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)), SLOT(sorted()));
    15. }
    16.  
    17. signals:
    18. void selectedRowChanged(const QString & row);
    19.  
    20. private slots:
    21. void evalRow(const QModelIndex & index)
    22. {
    23. QString row = "<not selected>";
    24. QAbstractItemModel * m = model();
    25.  
    26. if(m)
    27. {
    28. row = QString::number(countRow(index));
    29. mIndex = index;
    30. }
    31.  
    32. emit selectedRowChanged(row);
    33. }
    34.  
    35. void sorted()
    36. {
    37. evalRow(mIndex);
    38. }
    39.  
    40. private:
    41.  
    42. int countRow(const QModelIndex & index)
    43. {
    44. int count = 0;
    45.  
    46. if(index.isValid())
    47. {
    48. count = (index.row() + 1) + countRow(index.parent());
    49.  
    50. const QModelIndex parent = index.parent();
    51. if(parent.isValid())
    52. {
    53. for(int r = 0; r < index.row(); ++r)
    54. count += model()->rowCount(parent.child(r, 0));
    55. }
    56. }
    57.  
    58. return count;
    59. }
    60.  
    61. void AddItems()
    62. {
    63. QTreeWidgetItem * root = new QTreeWidgetItem(QStringList() << "root");
    64. QTreeWidgetItem * sub_1 = new QTreeWidgetItem(QStringList() << "sub_1");
    65. QTreeWidgetItem * sub_1_1 = new QTreeWidgetItem(QStringList() << "sub_1_1");
    66. QTreeWidgetItem * sub_1_2 = new QTreeWidgetItem(QStringList() << "sub_1_2");
    67. QTreeWidgetItem * sub_2 = new QTreeWidgetItem(QStringList() << "sub2");
    68. QTreeWidgetItem * sub_3 = new QTreeWidgetItem(QStringList() << "sub3");
    69.  
    70. root->addChild(sub_1);
    71. root->addChild(sub_2);
    72. root->addChild(sub_3);
    73.  
    74. sub_1->addChild(sub_1_1);
    75. sub_1->addChild(sub_1_2);
    76.  
    77. addTopLevelItem(root);
    78. }
    79. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 27th March 2017 at 11:18.
    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.

  13. The following user says thank you to Santosh Reddy for this useful post:

    PeterSilie (28th March 2017)

  14. #11
    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
    Are you sure, did you run the code I posted. It considers the new sorting order, just re-sort and click the item again. Refresh may be an issue....
    Well no i did not run your code, i just used the countRow-function in my code
    that is what i really need, the rest is not neccessary for me since i am not using this function for an item which gets clicked, it should be applicabale for any item whether is is selected or not.

    With your code my index function looks like this now:

    Qt Code:
    1. int indexOfItem(QTreeWidgetItem* item){
    2. QModelIndex index = indexFromItem(item);
    3. return countRow(index);
    4. }
    To copy to clipboard, switch view to plain text mode 

    But as i said it still does not consider the current sorting in the qtreewidget

  15. #12
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 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.

  16. #13
    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

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