row of item in QTreeWidget
-
Hi!
I have QTreeWidget, it is 2 level Tree (top level items, and their children). I click on childern item, and want to obtain its row index. For example I have 1 top level item, and 2 childern, I click on the second childern and want to get int=1. It should be quite easy, but I am a little bit confused yet with that model/view stuff. So could you please help?
-
Hi
TreeWidget is itembased and it seems that
currentindex().row() is not really working
as one would expect. (at least not for me. always zero)I ended up using treeWidget->currentItem() which gives the item.
Maybe you can use that too? -
If you connect to the
clicked()
signal of the QTreeWidget you can use the index parameter to get what you want, e.g.connect(treeWidget, &QTreeWidget::clicked, [](const QModelIndex &index) { qDebug() << index.row(); });
-
@mrjj Well, tnx for your answer, currentIndex().row() works! I don't know why it didn't work in your case. But still it looks a little bit ugly, i don't undertand why i can't get index from QTreeWidgetItem directly. There is QTreeWidget.IndexFromItem(QTreeWidgetItem* ) method, but it is protected!
-
@Harb
It did ? Nice.
That is good to know.
Must have done something wrong with mine.When I fooled around with TreeWidget it seems to be that - while its item based, it
still uses a tree model internal and hence it does not have a flat index but uses the
QModelIndex Class index feature.Can I ask what you need the index for ?
-
@mrjj Well, I need indexes to identify what part of a file I want ot load. My Top Level items in my QTreeWidget are names of files, and child items are "items" in the files, basically for each "item" relate big ammount of data into the file. I don't want to load whole file into the memory because it can be very large(40-50 Mb), so I just give user an interfces, so he can load just a part of a data, which he needs now.