解决PyQt5中 QFileSystemModel(),setRootPath()不起作用

from PyQt5.QtWidgets import QTreeView,QFileSystemModel,QApplication

class Main(QTreeView):

def __init__(self):

QTreeView.__init__(self)

model = QFileSystemModel()

dir='/home/'

model.setRootPath(dir)

self.setModel(model)

self.setRootIndex(model.index(dir))

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_())


加入self.setRootIndex(model.index(dir))

你可能感兴趣的:(解决PyQt5中 QFileSystemModel(),setRootPath()不起作用)