PDA

View Full Version : Get Row of specific QTreeWidgetItem in my QWidgetTree



PeterSilie
13th March 2017, 15:03
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.

Santosh Reddy
14th March 2017, 06:37
int QTreeWidget::indexOfTopLevelItem(QTreeWidgetItem *item) const

PeterSilie
23rd March 2017, 16:47
int QTreeWidget::indexOfTopLevelItem(QTreeWidgetItem *item) const

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]) :



int indexOfItem(QTreeWidgetItem* item){
int index = 0;
parent = item->parent();
if(parent != 0){
index += parent->indexOfChild(item)+1 + indexOfItem(parent);
}

return index;
}

Santosh Reddy
24th March 2017, 03:45
root
--sub1
----sub1.1
----sub1.2 <-- get the index for this, shoud be 3
--sub2
--sub3
[/CODE]

Please explain why 3 ?

d_stranz
24th March 2017, 17:06
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.

Santosh Reddy
27th March 2017, 04:33
If you are confused...
No I am not, just trying to understand why OP thinks so. :)

PeterSilie
27th March 2017, 07:40
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





int indexOfItem(QTreeWidgetItem* item){
int index = 0;
parent = item->parent();
if(parent != 0){
index += parent->indexOfChild(item)+1 + indexOfItem(parent);
}

return index;
}




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.

Santosh Reddy
27th March 2017, 08:33
Here is an example, see if this is what you want. Click the item and it's row number will appear above.



#include <QtWidgets>
// TreeWidget.h
class TreeWidget : public QTreeWidget
{
Q_OBJECT
public:
explicit TreeWidget(QWidget * parent = 0)
: QTreeWidget(parent)
{
setColumnCount(1);
AddItems();
setSortingEnabled(true);
connect(this, SIGNAL(clicked(QModelIndex)), SLOT(evalRow(QModelIndex)));
}

signals:
void selectedRowChanged(const QString & row);

private slots:
void evalRow(const QModelIndex & index)
{
QString row = "<not selected>";
QAbstractItemModel * m = model();

if(m)
row = QString::number(countRow(index));

emit selectedRowChanged(row);
}

private:
int countRow(const QModelIndex & index)
{
int count = 0;

if(index.isValid())
{
count = (index.row() + 1) + countRow(index.parent());

const QModelIndex parent = index.parent();
if(parent.isValid())
{
for(int r = 0; r < index.row(); ++r)
count += model()->rowCount(parent.child(r, 0));
}
}

return count;
}

void AddItems()
{
QTreeWidgetItem * root = new QTreeWidgetItem(QStringList() << "root");
QTreeWidgetItem * sub_1 = new QTreeWidgetItem(QStringList() << "sub_1");
QTreeWidgetItem * sub_1_1 = new QTreeWidgetItem(QStringList() << "sub_1_1");
QTreeWidgetItem * sub_1_2 = new QTreeWidgetItem(QStringList() << "sub_1_2");
QTreeWidgetItem * sub_2 = new QTreeWidgetItem(QStringList() << "sub2");
QTreeWidgetItem * sub_3 = new QTreeWidgetItem(QStringList() << "sub3");

root->addChild(sub_1);
root->addChild(sub_2);
root->addChild(sub_3);

sub_1->addChild(sub_1_1);
sub_1->addChild(sub_1_2);

addTopLevelItem(root);
}
};




// main.cpp
#include "TreeWidget.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QLabel * label = new QLabel;
TreeWidget * treeWidget = new TreeWidget;

QWidget mainWidget;

QVBoxLayout * layout = new QVBoxLayout(&mainWidget);
layout->addWidget(label);
layout->addWidget(treeWidget);

QObject::connect(treeWidget, SIGNAL(selectedRowChanged(QString)), label, SLOT(setText(QString)));

mainWidget.show();

return app.exec();
}

PeterSilie
27th March 2017, 10:15
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.

Santosh Reddy
27th March 2017, 10:21
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


#include <QtWidgets>

class TreeWidget : public QTreeWidget
{
Q_OBJECT
public:
explicit TreeWidget(QWidget * parent = 0)
: QTreeWidget(parent)
{
setColumnCount(1);
AddItems();
setSortingEnabled(true);
connect(this, SIGNAL(clicked(QModelIndex)), SLOT(evalRow(QModelIndex)));
connect(this->header(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)), SLOT(sorted()));
}

signals:
void selectedRowChanged(const QString & row);

private slots:
void evalRow(const QModelIndex & index)
{
QString row = "<not selected>";
QAbstractItemModel * m = model();

if(m)
{
row = QString::number(countRow(index));
mIndex = index;
}

emit selectedRowChanged(row);
}

void sorted()
{
evalRow(mIndex);
}

private:
QPersistentModelIndex mIndex;

int countRow(const QModelIndex & index)
{
int count = 0;

if(index.isValid())
{
count = (index.row() + 1) + countRow(index.parent());

const QModelIndex parent = index.parent();
if(parent.isValid())
{
for(int r = 0; r < index.row(); ++r)
count += model()->rowCount(parent.child(r, 0));
}
}

return count;
}

void AddItems()
{
QTreeWidgetItem * root = new QTreeWidgetItem(QStringList() << "root");
QTreeWidgetItem * sub_1 = new QTreeWidgetItem(QStringList() << "sub_1");
QTreeWidgetItem * sub_1_1 = new QTreeWidgetItem(QStringList() << "sub_1_1");
QTreeWidgetItem * sub_1_2 = new QTreeWidgetItem(QStringList() << "sub_1_2");
QTreeWidgetItem * sub_2 = new QTreeWidgetItem(QStringList() << "sub2");
QTreeWidgetItem * sub_3 = new QTreeWidgetItem(QStringList() << "sub3");

root->addChild(sub_1);
root->addChild(sub_2);
root->addChild(sub_3);

sub_1->addChild(sub_1_1);
sub_1->addChild(sub_1_2);

addTopLevelItem(root);
}
};

PeterSilie
27th March 2017, 12:14
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:



int indexOfItem(QTreeWidgetItem* item){
QModelIndex index = indexFromItem(item);
return countRow(index);
}


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

Santosh Reddy
27th March 2017, 12:34
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.

12406
12407
12408

PeterSilie
28th March 2017, 09:16
Yes it does work, trying running the code as is.

Changing the sorting order automatically changes the row number.

12406
12407
12408

Ok it is working now, had to clean and rebuild my project in order to make it work.

Thx alot

PeterSilie
30th March 2017, 14:57
Just in case somebody search for the same stuff again
here is my solution:




int rowsBelowIndex(const QModelIndex & index) {
int count = 0;
const QAbstractItemModel* model = index.model();
int rowCount = model->rowCount(index);
count += rowCount;
for (int r = 0; r < rowCount; ++r)
count += rowsBelowIndex(model->index(r, 0, index));
return count;

}

int calculateRow(const QModelIndex & index) {
int count = 0;
if (index.isValid()) {
count = (index.row()) + calculateRow(index.parent());

const QModelIndex parent = index.parent();
if (parent.isValid()) {
++count;
for (int r = 0; r < index.row(); ++r)
count += rowsBelowIndex(parent.child(r, 0));

}
}

return count;
}


int indexOfItem(QTreeWidgetItem* item) {
QModelIndex index = indexFromItem(item);
return calculateRow(index);
}