[PyQt] QDirmodel - Removing a directory and it's descendents..
Jebagnana Das
jebagnanadas at gmail.com
Sat Jan 16 16:43:38 GMT 2010
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.addToList)
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_12637fd44b798078&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!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100116/14048ac3/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 99780 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100116/14048ac3/attachment-0001.png>
More information about the PyQt
mailing list