EDIT2: model.hasChildren(parentIndex)
を返しますTrue
が、をmodel.rowCount(parentIndex)
返します0
。QFileSystemModelはPyQtの単なるfubarですか?
編集:少し調整すれば、これはすべて、QDirModelを使用した場合とまったく同じように機能します。これは非推奨ですが、QFileSystemModelがPyQtに完全に実装されていない可能性がありますか?
現在、Qt Model / Viewアーキテクチャを学習していますが、期待どおりに機能しないものを見つけました。私は次のコードを持っています(Qtモデルクラスから適応):
from PyQt4 import QtCore, QtGui
model = QtGui.QFileSystemModel()
parentIndex = model.index(QtCore.QDir.currentPath())
print model.isDir(parentIndex) #prints True
print model.data(parentIndex).toString() #prints name of current directory
rows = model.rowCount(parentIndex)
print rows #prints 0 (even though the current directory has directory and file children)
質問:
これはPyQtの問題ですか、何か間違ったことをしただけですか、それともQFileSystemModelを完全に誤解していますか?ドキュメントによるとmodel.rowCount(parentIndex)
、現在のディレクトリ内の子の数を返す必要があります。(私はこれをPython 2.6を搭載したUbuntuで実行しています)
QFileSystemModelのドキュメントには、Guiアプリケーションのインスタンスが必要であると記載されているため、上記のコードも次のようにQWidgetに配置しましたが、結果は同じです。
import sys
from PyQt4 import QtCore, QtGui
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
model = QtGui.QFileSystemModel()
parentIndex = model.index(QtCore.QDir.currentPath())
print model.isDir(parentIndex)
print model.data(parentIndex).toString()
rows = model.rowCount(parentIndex)
print rows
def main():
app = QtGui.QApplication(sys.argv)
widget = Widget()
widget.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()