我使用了post
中的部分代码(PyQt5)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_())
我对此行有疑问
model.setRootPath( 'C:\')
当我运行程序时,它总是显示像C:D的驱动器:只是不是C:\的内容,或者即使我输入“C:\ Users \”或者甚至不存在的路径,它总是只是显示,见附图,我做错了什么?
显示文件管理器的PyQt程序图像
我正在使用: Windows 10, PyCharm, Python 3.5, PyQt5,
感谢您的帮助。
答案 0 :(得分:1)
您必须使用QTreeView
向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_())