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:
QModelIndex currentTestStep = ui->treeWidget->currentIndex();
int y = currentTestStep.row();
And to get the modelIndex of the next item, you can use siblingAtRow(int row) as follows:
currentTestStep = currentTestStep.siblingAtRow(++y);
If you just need its row, then all you have to do is increment y
.
Be careful of incrementing y
past the number of children that your topLevelItem has, for example Test2 has only 3
children.
You can use itemBelow() after getting your currentItem :
QTreeWidget item = ui->treeWidget->itemBelow(ui->treeWidget->itemAt(currentTestStep->column(),currentTestStep->row()));
You can use just itemAt():
QTreeWidget item = ui->treeWidget->itemAt(currentTestStep->column(),++y));
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:
First, the QTreeWidget is a tree, not a list. Imagine the structure of a real tree. Starting at the root and trunk of the, it branches into a set of big limbs. Each of these big limbs splits into smaller limbs, and the limbs into branches, and so on, until you finally arrive at the leaves at the ends of the smallest branches.
If you consider any leaf in the tree, what "row" is it in? You can't say, because the tree is hierarchical.
However, you could say, "It is the fifth leaf on the second branch of the fourth limb off of the main trunk". This makes sense - if you assume there is some built-in order of branches. You start at the trunk, find the fourth limb (say moving clockwise from North). Once you find that, go to its second branch. The leaf you want is the fifth one.
If you translate this into programming terms, there is an ownership or parent/child relationship between all of the branches and leaves of the tree. The fifth leaf has a "parent", th second branch. The second branch has a "parent" in the fourth limb. The fourth limb's parent is the trunk (or root).
This is the structure used to represent a Qt tree in QTreeWidget or QTreeView. The QModelIndex is Qt's way of telling you exactly where you are in the tree. It works together with the QAbstractItemModel (which holds the information displayed in the tree) to help you navigate around the tree.
So, I think your second problem of understanding is that the "row" in QModelIndex is not the count of items displayed in the tree view counting from the top of the display. It refers to the "nth" child of whatever the QModelIndex's parent is. It is not an absolute position in the tree, it is relative to its parent.
In addition to each node in a Qt tree having one or more rows, each of these rows can have one or more columns in it (think of the tree in a file browser - for each file or directory, there is a list of attributes - name, date modified, size, permissions, etc.). These are the columns of the given row.
A QModelIndex therefore tells you what row you are in the tree with respect to the item's parent and what column you are in the row counting from the left. If you want to find your absolute position in the tree, you have to go up the tree by asking for the parent of your item until you finally reach the top (root) of the tree, which is an "invalid" QModelIndex (QModelIndex::isValid() returns false).