QTreeView :: scrollTo not working

Qt 4.8

I have a QTreeView based class with its associated QAbstractItemModel class. If I reload the model with new information, I want to expand / scroll the tree to the previous selected item.

Both clans, the tree structure and the model are correctly created and connected using QTreeView::setSelectionModel(...) , which works correctly.

After reloading the model, I get a valid index for the previous selected item, and I scroll through it:

 myTreeView->scrollTo(index); 

but the tree is not turning. However, if I expand the tree manually, the item is indeed selected.

The tree view is initialized with:

 header()->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel); header()->setStretchLastSection(false); header()->setResizeMode(0, QHeaderView::ResizeToContents); 

Any idea on expanding the tree to choose?

+4
source share
4 answers

I just dealt with a similar situation, setting the model index through setModelIndex (which ends with scrollTo inside) worked fine for one of my models, but bad for the other.

When I resolutely expanded all the elements of level 1, scrollTo worked the same way as described above (enough to call expandAll ).

The reason was a mistake in my model class in:

 QModelIndex MyBadModel::parent(const QModelIndex& index) const 

and, as I understand it, everything became normal. The error was such that the internalId index of the parent model was not the same as when the same model index (for the parent) is calculated "from a different direction", therefore, this model index (returned by the parent method) could not be found in the list of visual indexes.

+2
source

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); } 
+1
source

QTreeView::scrollTo should expand the hierarchy accordingly.

Your QModelIndex object probably becomes invalid when the model is updated (and maybe still selects the correct row, because the row information remains valid, although the original name is missing, don’t ask me how these internal elements work). From the QModelIndex document :

Note. Model indices should be used immediately and then discarded. You do not have to rely on indexes to remain valid after calling model functions that modify the structure of the model or remove elements. If you need to maintain the model index over time, use QPersistentModelIndex .

Of course, you can look into the QPersistentModelIndex object, but as he says in his documentation :

It is good practice to verify that indexes on persistent models are valid before they are used.

Otherwise, you can always request this item again after updating the model.

0
source

Everything was simple. Just change the autoExpandDelay property from -1 to 0 (for example).

 ui->treeView->setAutoExpandDelay(0); 
0
source

Source: https://habr.com/ru/post/1490311/


All Articles