I have a QTreeWidget in my project for which I want to get the index of its items.
My QTreeWidget is a list of tests that I want to perform. For example, Test 1, Test 2, Test 3, Test 4 etc.
Test 2 and Test 4 have children Test a, Test b, Test c etc. Once Test 1 is finished. I want the Test a of Test 2 to be highlighted in the tree.
I need to work with indexes of the items in my tree. I have tried the following to get index of the tree widget:
QModelIndex currentTestStep = ui->treeWidget->currentIndex();
Or:
QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget->currentItem());
int y = item->indexOfChild(ui->treeWidget->currentItem());
The QModelIndex and y were empty in debug window. What am I missing?
Your first method you used gets you the current's item modelIndex, and it seems that what you need is the item's row() so you can work with it.
To do that you need to do the following:
And to get the modelIndex of the next item, you can use siblingAtRow(int row) as follows:
If you just need its row, then all you have to do is increment
y.Be careful of incrementing
ypast the number of children that your topLevelItem has, for example Test2 has only3children.Another solution
You can use itemBelow() after getting your currentItem :
Another solution
You can use just itemAt():
There are multiple ways to do this, check out QTreeWidget documentation, and see if you can come up with another way to do what you need.
Here's an explanation of QTreeWidget's indexing that I found here, read it so that you don't run into problems while iterating through your treeWidget, or know what the problem is if you your program crashes: