2017-05-06 50 views
0

我見過有關QFileSystemModel rowCount不按預期工作的文章(ex1,ex2),但我似乎錯過了一些東西。下面的代碼總是報告1的rowCount,即使列表顯示more..even在等待10秒之後。我在這裏錯過了什麼?pyqt QFileSystemModel rowCount

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_()) 

...爲sanity..here是相同的代碼與相同的結果

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

PyQt4中你必須通過你要分析的項目的索引,如果你想知道如何您有很多項目,請使用返回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_()) 
+0

非常感謝你!..看起來很明顯,現在你指出了,但不知道我會想出來! –