pyqt QFileSystemModel rowCount
Posted
技术标签:
【中文标题】pyqt QFileSystemModel rowCount【英文标题】: 【发布时间】:2017-05-06 03:00:42 【问题描述】:我已经看到有关 QFileSystemModel rowCount 无法按预期工作的帖子(ex1、ex2),但我似乎遗漏了一些东西。以下代码始终报告 rowCount 为 1,即使列表显示更多……即使在等待 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_())
【问题讨论】:
【参考方案1】:你必须传递你要分析的项目的索引,如果你想知道你有多少项目,使用返回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_())
【讨论】:
非常感谢!..现在您指出这一点似乎很明显,但不确定我是否会想出来!以上是关于pyqt QFileSystemModel rowCount的主要内容,如果未能解决你的问题,请参考以下文章
pyqt Qtablewidget 获取选中多行的行号或者内容