[PyQt] QDirModel with QTreeView.. Help Please...

Russell Valentine russ at coldstonelabs.org
Wed Feb 10 23:52:34 GMT 2010


Maybe you want this in your slot?

self.tree.setRowHidden(index.row(), index.parent(), True)

I don't know how you can say you don't know what is wrong with your code 
below since in your slot you are only printing stuff and not doing 
anything. You probably want to reimplement QDirModel if you are wanting 
to do something more than hide the row.

Jebagnana Das wrote:
> Hai all,
> 
>         I've populated the directory tree using QDirModel. My aim is
> whenever i click on the patriculr folder or directory, the directory and
> it's children should be deleted from the model.. I don't know what am i
> doing wrong in the code below..
> 
> from PyQt4 import QtCore, QtGui
> 
> class Ui_Dialog(QtGui.QDialog):
> 
>     def __init__(self,parent=None):
>         QtGui.QDialog.__init__(self,parent)
>         self.setObjectName("Dialog")
>         self.resize(600, 500)
> 
> 
>         self.model = QtGui.QDirModel()
>         self.tree = QtGui.QTreeView()
>         self.tree.setModel(self.model)
>         print(self.model.flags(self.model.index("c:\Program Files")))
>         self.model.setFilter(QtCore.QDir.Dirs|QtCore.QDir.NoDotAndDotDot)
> 
> 
>         self.tree.setSortingEnabled(True)
> 
>         self.tree.setRootIndex(self.model.index("c:\Program Files"))
> 
>         #self.tree.hideColumn(1)
>         #self.tree.hideColumn(2)
>         #self.tree.hideColumn(3)
>         self.tree.setWindowTitle("Dir View")
>         self.tree.resize(400, 480)
>         self.tree.setColumnWidth(0,200)
> 
>         self.tree.show()
>         QtCore.QObject.connect(self.tree,
> QtCore.SIGNAL("clicked(QModelIndex)"), self.test)
>         QtCore.QMetaObject.connectSlotsByName(self)
> 
>         self.setWindowTitle(QtGui.QApplication.translate("Dialog",
> "Dialog", None, QtGui.QApplication.UnicodeUTF8))
> 
>     def test(self,index):
> 
> 
>         print(self.model.filePath(index))
> 
>         print(self.model.rowCount(index))
> 
>          #self.model.beginRemoveRows(index.parent(),index.row(),self.model.rowCount(index))
>         #self.model.endRemoveRows()
> 
>         print("Row of the index =",index.row())
> 
> 
>         print("Parent = ",self.model.data(index.parent()))
> 
> if __name__ == "__main__":
>     import sys
>     app = QtGui.QApplication(sys.argv)
>     ui = Ui_Dialog()
>     #ui.show()
>     sys.exit(app.exec_())
> 
> Any experts.. Please correct me as early as possible.. Thank you all..
> 



More information about the PyQt mailing list