Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python QFileSystemModel的QTreeView工作不正常_Python_Pyqt_Pyqt5_Qtreeview_Qfilesystemmodel - Fatal编程技术网

Python QFileSystemModel的QTreeView工作不正常

Python QFileSystemModel的QTreeView工作不正常,python,pyqt,pyqt5,qtreeview,qfilesystemmodel,Python,Pyqt,Pyqt5,Qtreeview,Qfilesystemmodel,我设置QFileSystemModel根路径,然后将其设置为QTreeView模型,但如果我尝试查找特定文件的索引,它会给我D: 我肯定文件在那里 self.model=qtwidts.QFileSystemModel() self.model.setNameFilters(['*.ma'])) self.model.setFilter(QtCore.QDir.Files)#QtCore.QDir.AllDirs | QtCore.QDir.NoDotAndDotDot | QtCore.QDi

我设置QFileSystemModel根路径,然后将其设置为QTreeView模型,但如果我尝试查找特定文件的索引,它会给我D: 我肯定文件在那里

self.model=qtwidts.QFileSystemModel()
self.model.setNameFilters(['*.ma']))
self.model.setFilter(QtCore.QDir.Files)#QtCore.QDir.AllDirs | QtCore.QDir.NoDotAndDotDot | QtCore.QDir.AllEntries)
self.model.setNameFilterDisables(False)
self.model.setRootPath(路径)
self.tree\u local\u file.setModel(self.model)
self.tree\u local\u file.setRootIndex(self.model.index(path))
# ...
#然后
# ...
对于范围内的i(self.model.rowCount()):
index=self.model.index(i,0)
file_name=str(self.model.fileName(index))
file_path=str(self.model.filePath(索引))
打印(文件路径)#这给了我->D:/
如果文件名==主文件名:
self.tree\u local\u file.setCurrentIndex(索引)
self.open_文件()
打破
#或
index=(self.model.index(主文件名[1]))
打印(self.model.filePath(index))#这没有给我任何信息
如果已审核:

QModelIndex QFileSystemModel::setRootPath(常量QString和newPath)

将模型监视的目录设置为newPath by 在其上安装文件系统监视程序。对文件和文件的任何更改 此目录中的目录将反映在模型中

如果路径发生更改,将发出rootPathChanged()信号

注:此功能不会改变模型或组件的结构 修改视图可用的数据。换言之,“根”的 模型不会更改为仅包含模型中的文件和目录 文件系统中newPath指定的目录。

(强调矿山)

据了解,模型的根从未更改,因此如果要访问根路径下的项,必须获取与该路径关联的QModelIndex,然后获取子项

另一方面,QFileSystemModel在另一个线程中执行其任务,以避免GUI的某些阻塞,因此在更改根路径时,您将无法获得适当的路由,但至少您必须等待发出directoryLoaded信号,指示在该线程上完成的工作已结束

考虑到上述情况,一个可能的解决方案是:

从PyQt5导入QtCore、QtWidgets
类MainWindow(QtWidgets.QMainWindow):
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
self.tree\u local\u file=qtwidts.QTreeView()
self.setCentralWidget(self.tree\u本地\u文件)
path=“/foo/path/”
self.model=qtwidts.QFileSystemModel()
self.model.setNameFilters([“*.ma”]))
self.model.setFilter(
QtCore.QDir.Files
)#QtCore.QDir.AllDirs | QtCore.QDir.NoDotAndDotDot | QtCore.QDir.AllEntries)
self.model.setNameFilterDisables(False)
self.model.setRootPath(路径)
self.tree\u local\u file.setModel(self.model)
self.tree\u local\u file.setRootIndex(self.model.index(path))
self.model.directoryLoaded.connect(self.onDirectoryLoaded)
@QtCore.pyqtSlot()
def onDirectoryLoaded(自):
root=self.model.index(self.model.rootPath())
对于范围内的i(self.model.rowCount(root)):
index=self.model.index(i,0,根)
文件名=self.model.fileName(索引)
file\u path=self.model.filePath(索引)
打印(文件路径)
如果名称=“\uuuuu main\uuuuuuuu”:
导入系统
app=qtwidts.QApplication(sys.argv)
w=主窗口()
w、 show()
sys.exit(app.exec_())