Reputation: 31
I have used part of a code (PyQt5) from this post
from PyQt5.QtWidgets import QTreeView,QFileSystemModel,QApplication
class Main(QTreeView):
def __init__(self):
QTreeView.__init__(self)
model = QFileSystemModel()
model.setRootPath('C:\\')
self.setModel(model)
self.doubleClicked.connect(self.test)
def test(self, signal):
file_path=self.model().filePath(signal)
print(file_path)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
And i have a problem with this line
model.setRootPath('C:\')
When i run the program it always shows drives like C: D: just not the content of C:\ or even if i type "C:\Users\" or a path that doesn't even exists, it always just shows, see attached image, What am i doing wrong?
Image of PyQt Program showing file manager
I am using: Windows 10, PyCharm, Python 3.5, PyQt5,
Thanks for your help.
Upvotes: 2
Views: 1493
Reputation: 244301
You must indicate to the QTreeView
what is your root item with setRootIndex()
:
from PyQt5.QtCore import QDir
from PyQt5.QtWidgets import QTreeView,QFileSystemModel,QApplication
class Main(QTreeView):
def __init__(self):
QTreeView.__init__(self)
model = QFileSystemModel()
self.setModel(model)
model.setRootPath(QDir.rootPath())
self.setRootIndex(model.index("C:"))
self.doubleClicked.connect(self.test)
def test(self, signal):
file_path=self.model().filePath(signal)
print(file_path)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
Upvotes: 3