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

    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.

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

    PeterSilie (28th March 2017)

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

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

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

    PeterSilie (28th March 2017)

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

  7. #5
    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.

  8. #6
    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

  9. #7
    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.