Even the QTreeView::scrollTo says:
Scroll the contents of the tree view until the given model item index is visible. The hint parameter specifies more precisely where the item should be located after the operation. If any of the parents of the model item are collapsed, they will be expanded to ensure that the model item is visible.
This is not true (I think)
If the problem is solved, then the problem is to manually expand all previous levels of the tree:
// This slot is invoqued from model using last selected item void MyTreeWidget::ItemSelectedManually(const QModelIndex & ar_index) { std::vector<std::pair<int, int> > indexes; // first of all, I save all item "offsets" relative to its parent QModelIndex indexAbobe = ar_index.parent(); while (indexAbobe.isValid()) { indexes.push_back(std::make_pair(indexAbobe.row(), indexAbobe.column())); indexAbobe = indexAbobe.parent(); } // now, select actual selection model auto model = _viewer.selectionModel()->model(); // get root item QModelIndex index = model->index(0, 0, QModelIndex()); if (index.isValid()) { // now, expand all items below for (auto it = indexes.rbegin(); it != indexes.rend() && index.isValid(); ++it) { auto row = (*it).first; auto colum = (*it).second; _viewer.setExpanded(index, true); // and get a new item relative to parent index = model->index(row, colum, index); } } // finally, scroll to real item, after expanding everything above. _viewer.scrollTo(ar_index); }
source share