2014-12-19 40 views
3

最近幾個小時我一直在閱讀,但沒有找到一個很好的解決方案,看似簡單的常見問題。 我有一個QFileSystemModel的QTreeView。我想將當前索引設置爲用戶保存的最後一個文件並滾動到該位置。 因爲qfilesystemmodel負載以異步的方式,如果我立刻使用功能scrollTo(mydesiredindex),像這樣:使用QFileSystemModel在啓動時滾動到QTreeView中的文件

Model = new QFileSystemModel; 
Model->setRootPath(RootDirectory); 
Model->setFilter(QDir::Dirs | QDir::NoDotAndDotDot); 
ui.RootView->setModel(Model); 
ui.RootView->setCurrentIndex(Model->index(LastUsedPath)); 
ui.RootView->scrollTo(Model->index(LastUsedPath)); 

的QTreeView則滾動到文件的當前位置,但後來增加了更多的文件之前,使mydesiredindex被推出視圖。

我試圖得到一個信號,模型完成填充樹視圖,但無濟於事。 signalsLoaded(const QString &)和rowsInserted(const QModelIndex &,int,int))在模型完成填充之前發出信號。

感謝任何人的幫助。

回答

1

我相信它可能與您的命令的順序有關。我責令如下

self.tree.scrollTo(index) 
self.tree.expand(index) 
self.tree.setCurrentIndex(index) 

或者在你的代碼

ui.RootView->scrollTo(Model->index(LastUsedPath)); 
ui.RootView->expand(Model->index(LastUsedPath)); 
ui.RootView->setCurrentIndex(Model->index(LastUsedPath)); 

希望它能幫助。

相關問題