tnrgus
tnrgus

Reputation: 365

How to scroll to current index?

I am making a file browser using QTreeView and QFileSystemModel. It must be selected and scroll the tree to a selected item automatically. But it doesn't work scroll automatically without getting keyboard.

QString strFile = "/usr/bin/qmake";

QFileInfo fi(strFile);
QString dirFile = fi.dir().absolutePath();

QFileSystemModel model;
model.setRootPath("/");
model.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);

QTreeView view;
view.setModel(&model);
tree.setCurrentIndex(model.index(strFile));
tree.scrollTo(model.index(strFile));

view.show();

When executing above code, scroll is top but file is selected.

What's the problem not to scroll to selected item?

Upvotes: 0

Views: 1073

Answers (1)

Zlatomir
Zlatomir

Reputation: 7034

You can use scrollTo

view.scrollTo(view.currentIndex()); 

Optional pass the option where you want your item as a second parameter, like: QAbstractItemView::PositionAtTop or other

LE: i noticed that you actually call scrollTo, but you call it for some tree (that we can't see what it is) you should call that for view, same applies for setCurrentIndex.

Upvotes: 3

Related Questions