6
我知道如何使用QDirModel(或QFileSystemModel)來製作一個簡單的QTreeView()來顯示系統中的文件/文件夾,但我想添加一個複選框旁邊的每一個所以用戶可以在他的系統上選擇一些文件夾/文件。顯然,我也需要知道他選擇了哪些。任何提示?PyQt4本地目錄視圖與選擇文件夾的選項
基本上是這樣的......
下面是一個示例代碼,使一個目錄視圖,但沒有對應的複選框。
from PyQt4 import QtGui
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
model = QtGui.QDirModel()
tree = QtGui.QTreeView()
tree.setModel(model)
tree.setAnimated(False)
tree.setIndentation(20)
tree.setSortingEnabled(True)
tree.setWindowTitle("Dir View")
tree.resize(640, 480)
tree.show()
sys.exit(app.exec_())
這工作得很好,但你知道如何所以當一個人點擊基於C修改:那麼所有的子類別獲得點擊和時點擊一個子類別C:成爲一個點(半檢查)? – Kiarash 2013-03-14 23:42:35
@Kiarash你應該在'setData'中相應地改變'index'的子和父索引。要獲取子索引,請使用'QModelIndex.child'。要獲取父索引,請使用'QModelIndex.parent'。要獲得一定數量的給定索引的孩子,你應該使用'QDirModel.rowCount'。見[this](http://pyqt.sourceforge.net/Docs/PyQt4/qmodelindex.html)和[this](http://pyqt.sourceforge.net/Docs/PyQt4/qabstractitemmodel.html#rowCount) – user2155932 2013-03-15 04:53:50
所以在setData中,我只是添加了這個:對於我在範圍內(self.rowCount(index)):self.setData(index.child(i,0),value,role)但是如果點擊C:任何想法如何解決這個問題? – Kiarash 2013-04-10 04:13:01