Python pyqt QFileSystemModel行数
我看到过关于QFileSystemModel行数没有按预期工作(,)的帖子,但我似乎遗漏了一些东西。以下代码始终报告行数为1,即使列表显示更多..即使在等待10秒后。我错过了什么Python pyqt QFileSystemModel行数,python,python-3.x,pyqt,pyqt4,pyqt5,Python,Python 3.x,Pyqt,Pyqt4,Pyqt5,我看到过关于QFileSystemModel行数没有按预期工作(,)的帖子,但我似乎遗漏了一些东西。以下代码始终报告行数为1,即使列表显示更多..即使在等待10秒后。我错过了什么 import os, sys from PyQt5 import QtWidgets, QtCore class TestWindow(QtWidgets.QMainWindow): def __init__(self): QtWidgets.QMainWindow.__init__(sel
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_())