gpt4 book ai didi

python - pyqt QFileSystemModel rowCount

转载 作者:太空宇宙 更新时间:2023-11-03 15:16:50 25 4
gpt4 key购买 nike

我看过有关 QFileSystemModel rowCount 未按预期工作的帖子( ex1ex2 ),但我似乎遗漏了一些东西。以下代码始终报告 rowCount 为 1,即使列表显示 more..甚至在等待 10 秒后也是如此。我在这里缺少什么?

import os, sys
from PyQt5 import QtWidgets, QtCore


class TestWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.model = QtWidgets.QFileSystemModel()
self.model.setFilter(QtCore.QDir.AllEntries | QtCore.QDir.Hidden | QtCore.QDir.NoDot)
self.path = os.path.expanduser('~')
self.model.setRootPath(self.path)
view = QtWidgets.QListView()
view.setModel(self.model)
view.setRootIndex(self.model.index(self.path))
self.setCentralWidget(view)
self.model.directoryLoaded.connect(self._loaded)
QtCore.QTimer.singleShot(10000, self._really_loaded)

def _loaded(self):
print('_loaded', self.path, self.model.rowCount()) # Always returns 1 here? even though there are more rows displayed

def _really_loaded(self):
print('_really_loaded', self.path, self.model.rowCount()) # 10 seconds later...Always returns 1 here? even tho there are more rows displayed


if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
test = TestWindow()
test.show()
sys.exit(app.exec_())

...为了理智......这里的代码与 pyqt4 相同,结果相同

import os, sys
from PyQt4 import QtGui, QtCore


class TestWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.model = QtGui.QFileSystemModel()
self.model.setFilter(QtCore.QDir.AllEntries | QtCore.QDir.Hidden | QtCore.QDir.NoDot)
self.path = os.path.expanduser('~')
self.model.setRootPath(self.path)
view = QtGui.QListView()
view.setModel(self.model)
view.setRootIndex(self.model.index(self.path))
self.setCentralWidget(view)
self.model.directoryLoaded.connect(self._loaded)
QtCore.QTimer.singleShot(10000, self._really_loaded)

def _loaded(self):
print('_loaded', self.path, self.model.rowCount()) # Always returns 1 here? even though there are more rows displayed

def _really_loaded(self):
print('_really_loaded', self.path, self.model.rowCount()) # 10 seconds later...Always returns 1 here? even tho there are more rows displayed


if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
test = TestWindow()
test.show()
sys.exit(app.exec_())

最佳答案

您必须传递要分析的项目的索引,如果您想知道您有多少个项目,请使用返回 setRootPath() 的索引。

import os, sys
from PyQt5 import QtWidgets, QtCore


class TestWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.model = QtWidgets.QFileSystemModel()
self.model.setFilter(QtCore.QDir.AllEntries | QtCore.QDir.Hidden | QtCore.QDir.NoDot)
self.path = os.path.expanduser('~')
self.parentIndex = self.model.setRootPath(self.path)
view = QtWidgets.QListView()
view.setModel(self.model)
view.setRootIndex(self.model.index(self.path))
self.setCentralWidget(view)
self.model.directoryLoaded.connect(self._loaded)
QtCore.QTimer.singleShot(10000, self._really_loaded)

def _loaded(self, path):
print('_loaded', self.path, self.model.rowCount(self.parentIndex)) # Always returns 1 here? even though there are more rows displayed

def _really_loaded(self):
print('_really_loaded', self.path, self.model.rowCount(self.parentIndex)) # 10 seconds later...Always returns 1 here? even tho there are more rows displayed


if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
test = TestWindow()
test.show()
sys.exit(app.exec_())

关于python - pyqt QFileSystemModel rowCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43816264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com