I wonder if anyone could tell me where I am going wrong. I am trying to open a Treeview with the previously selected folder, Expanded, highlighted and scrolled into view. I have succeeded with the first two options. The issue is the scroll doesn't work and you have to manually scroll down to find the expanded/highlighted folder.
From my understanding I need to pass an index into scrollto ,however this doesn't work.
index = self.tree.currentIndex()
self.tree.scrollTo(index, QAbstractItemView.PositionAtBottom)
Full simple example below. Please add a folder far down your hard drive to demonstrate the issue correctly. Using self.last_opened_folder = "YOUR DIR"
Thank you
import sys
from PyQt5.QtWidgets import QApplication, QFileSystemModel, QTreeView, QWidget, QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton,
QToolTip, QMessageBox, QLabel)
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'TEST'
self.left = 0
self.top = 0
self.width = 640
self.height = 480
self.initUI()
def initUI(self):
self.last_opened_folder = "E:/z/1" # far down folder
#
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
# Call Model
self.model = QFileSystemModel(self)
self.model.setRootPath("")
self.model.setFilter(QtCore.QDir.AllDirs|QtCore.QDir.NoDotAndDotDot) # filters out directories only
# Make Tree and load model
self.tree = QTreeView(self)
self.model.setRootPath('')
self.tree = QTreeView()
self.tree.setModel(self.model)
self.tree.setExpanded(self.model.index(self.last_opened_folder), True)
self.tree.setCurrentIndex(self.model.index(self.last_opened_folder)) # highlight
index = self.tree.currentIndex()
self.tree.scrollTo(index, QAbstractItemView.PositionAtBottom)
# Format
self.tree.expandAll()
self.tree.setColumnWidth(0, 450)
self.tree.setColumnHidden(1, True)
self.tree.setAnimated(False)
self.tree.setIndentation(20)
self.tree.sortByColumn(0, Qt.AscendingOrder)
windowLayout = QVBoxLayout()
windowLayout.addWidget(self.tree)
self.setLayout(windowLayout)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())