I use setCurrentItem() to focus on a specific item,but the QTreeWidget does not jump to the specific item correspondingly. Are there any function of QTreeWidget can do this?
Asked
Active
Viewed 1,690 times
2 Answers
2
Yes, you can use the scrollTo(const QModelIndex &index, ScrollHint hint = EnsureVisible)
function (documentation link).
The index
parameter is the index of the item that you want to bring into view
The hint
parameter specifies where the item should be located after the operation (e.g. on top, in the middle, etc).
QTreeWidget w;
w.setCurrentItem(<your item>);
w.scrollTo(w.indexFromItem(<your item>)); // or w.scrollTo(w.currentIndex())

SingerOfTheFall
- 29,228
- 8
- 68
- 105
-
I only use the class of QTreeWidget,can I use this scrollTo function directly like this:`FaceLibTree->setCurrentItem(item); QModelIndex index = FaceLibTree->currentIndex(); FaceLibTree->scrollTo(index);` Its doesnt work. – phuinews Jan 17 '17 at 06:23
-
@phuinews, QTreeWidget is derived from QTreeView – SingerOfTheFall Jan 17 '17 at 06:23
-
@ SingerOfTheFall,Thanks very much,I dont know this before! But I still dont know how to use the function to let the QTreeWidget jump to the currentItem. – phuinews Jan 17 '17 at 06:33
-
@ SingerOfTheFall,Sorry,It works,I just used the wrong ScrollHint parameter.Thanks. – phuinews Jan 17 '17 at 07:09
-
QTreeWidget has scrollToItem() – martial Sep 01 '21 at 13:12
0
You can use scrollToItem() : Link to the Qt doc : https://doc.qt.io/qt-5/qtreewidget.html#scrollToItem

martial
- 136
- 2
- 8
-
This doesn't answer the question of how to do it. `scrollTo` requires an index, and there's no simple way to get an index for a given `QTreeWidgetItem`. – Violet Giraffe Mar 02 '23 at 22:27