[PyQt] QDirmodel - Removing a directory and it's descendents..

detlev detlev at die-offenbachs.de
Sun Jan 17 13:42:45 GMT 2010


On Sonntag, 17. Januar 2010, Jebagnana Das wrote:
> Thanks detlev for your suggestion.. Could you please give me any example
> code snippet?. Bcoz. i'm totally new to model/view programming and pyqt.
> 
> On Sun, Jan 17, 2010 at 5:03 PM, detlev <detlev at die-offenbachs.de> wrote:
> > Hi,
> >
> > how about using a QSortFilterProxy model to filter the excluded ones?
> >
> > Detlev
> >
> > On Samstag, 16. Januar 2010, Jebagnana Das wrote:
> > > Hello all,
> > >         Greetings.. After a spending a long time for googling i found
> > > the way to list the contents of a particular directory. Thanks to
> > > QDirModel.
> > >
> > >        My aim here is to allow the user to exclude specific directories
> > > within the chosen directory.. After adding a particular directory to
> > > the exclusion list(on the right side)the selected directory and it's
> > > descendents should be removed(or hidden)from the tree view so that the
> > > user is not allowed to select the same folder again..
> > >
> > >       Given here the minimal example needed for my app.. In this
> > > example when the user presses add to exclude list push button the
> > > folder is added to the exclude folders listwidget..
> > >
> > > from PyQt4 import QtCore, QtGui
> > >
> > > class Ui_Dialog(QtGui.QDialog):
> > >
> > >     def __init__(self,parent=None):
> > >         QtGui.QDialog.__init__(self,parent)
> > >         self.resize(600, 500)
> > >
> > >         self.model = QtGui.QDirModel()
> > >         self.tree = QtGui.QTreeView(self)
> > >         self.tree.setModel(self.model)
> > >        
> > > self.model.setFilter(QtCore.QDir.Dirs|QtCore.QDir.NoDotAndDotDot)
> > > self.tree.setSortingEnabled(True)
> >
> > self.tree.setRootIndex(self.model.index("/home/jebagnanadasa/Python-3.1.1
> >")
> >
> > > )
> > >
> > >         self.tree.hideColumn(1)
> > >         self.tree.setWindowTitle("Dir View")
> > >         self.tree.resize(400, 480)
> > >         self.tree.setColumnWidth(0,150)
> > >         self.tree.show()
> > >
> > >         self.pushButton = QtGui.QPushButton(self)
> > >         self.pushButton.setGeometry(QtCore.QRect(420, 30, 151, 28))
> > >         self.listWidget = QtGui.QListWidget(self)
> > >         self.listWidget.setGeometry(QtCore.QRect(410, 80, 171, 231))
> > >
> > >         QtCore.QObject.connect(self.tree,
> > > QtCore.SIGNAL("clicked(QModelIndex)"), self.test)
> >
> > QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL('clicked()'),self.ad
> >dT
> >
> > > oList) QtCore.QMetaObject.connectSlotsByName(self)
> > >
> > >         self.setWindowTitle(QtGui.QApplication.translate("Dialog",
> > >  "Dialog", None, QtGui.QApplication.UnicodeUTF8))
> > >         self.pushButton.setText(QtGui.QApplication.translate("Dialog",
> >
> > "Add
> >
> > > to Exclude List", None, QtGui.QApplication.UnicodeUTF8))
> > >
> > >     def test(self,index):
> > >
> > >         if self.model.data(index.parent())=="jebagnanadasa":
> > >             self.selectedDirectoryPath=self.model.data(index)
> > >             print(self.selectedDirectoryPath)
> > >         else:
> > >             dirHierarchy=[]
> > >             dirHierarchy.insert(0,self.model.data(index))
> > >             while (self.model.data(index.parent())!="jebagnanadasa"):
> > >                 index=index.parent()
> > >                 dirHierarchy.insert(0,self.model.data(index))
> > >             self.selectedDirectoryPath="/".join(dirHierarchy)
> > >             print(self.selectedDirectoryPath)
> > >
> > >         #self.model.removeRow(0,index)
> > >
> > >     def addToList(self):
> > >         self.listWidget.addItem(self.selectedDirectoryPath)
> > >
> > >
> > > if __name__ == "__main__":
> > >     import sys
> > >     app = QtGui.QApplication(sys.argv)
> > >     ui = Ui_Dialog()
> > >     ui.show()
> > >     sys.exit(app.exec_())
> > >
> > > See the screenshot also..
> > >
> > > [image:
> >
> > ?ui=2&view=att&th=12637fd44b798078&attid=0.1&disp=attd&realattid=ii_12637
> >fd
> >
> > > 44b798078&zw]
> > >
> > > Is there a way that i can make the excluded directories to be ignored
> >
> > when
> >
> > > displaying the contents of the directory on consecutive startup(since
> > > it was already on the exclusion list)?
> > >
> > > Any help would be much appreciated.. Thanks..
> > >
> > > P.S: remove() and rmdir() is not recommended since it deletes the
> > > directories entirely from the file System!
> >
> > --
> > Detlev Offenbach
> > detlev at die-offenbachs.de
> 

Haven't used one with QDirModel yet but eric4/5 contain quite a bunch of 
QSortFilterModel usage examples. The Qt docu isn't too bad either.

Regards,
Detlev
-- 
Detlev Offenbach
detlev at die-offenbachs.de


More information about the PyQt mailing list