Hello all,<br> Greetings.. After a spending a long time for googling i found the way to list the contents of a particular directory. Thanks to QDirModel.<br><br> 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)<span>the selected directory and it's </span><span>descendents should be removed(or hidden)from the tree view so that the user is not allowed to select the same folder again.. <br>
<br> 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..<br><br>from PyQt4 import QtCore, QtGui<br>
<br>class Ui_Dialog(QtGui.QDialog):<br> <br> def __init__(self,parent=None):<br> QtGui.QDialog.__init__(self,parent)<br> self.resize(600, 500)<br><br> self.model = QtGui.QDirModel()<br> self.tree = QtGui.QTreeView(self)<br>
self.tree.setModel(self.model)<br> self.model.setFilter(QtCore.QDir.Dirs|QtCore.QDir.NoDotAndDotDot)<br> self.tree.setSortingEnabled(True)<br> self.tree.setRootIndex(self.model.index("/home/jebagnanadasa/Python-3.1.1"))<br>
<br> self.tree.hideColumn(1)<br> self.tree.setWindowTitle("Dir View")<br> self.tree.resize(400, 480)<br> self.tree.setColumnWidth(0,150)<br> self.tree.show()<br><br> self.pushButton = QtGui.QPushButton(self)<br>
self.pushButton.setGeometry(QtCore.QRect(420, 30, 151, 28))<br> self.listWidget = QtGui.QListWidget(self)<br> self.listWidget.setGeometry(QtCore.QRect(410, 80, 171, 231))<br><br> QtCore.QObject.connect(self.tree, QtCore.SIGNAL("clicked(QModelIndex)"), self.test)<br>
QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL('clicked()'),self.addToList)<br> QtCore.QMetaObject.connectSlotsByName(self)<br> <br> self.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))<br>
self.pushButton.setText(QtGui.QApplication.translate("Dialog", "Add to Exclude List", None, QtGui.QApplication.UnicodeUTF8))<br><br> def test(self,index):<br> <br> if self.model.data(index.parent())=="jebagnanadasa":<br>
self.selectedDirectoryPath=self.model.data(index)<br> print(self.selectedDirectoryPath)<br> else:<br> dirHierarchy=[]<br> dirHierarchy.insert(0,self.model.data(index))<br>
while (self.model.data(index.parent())!="jebagnanadasa"):<br> index=index.parent()<br> dirHierarchy.insert(0,self.model.data(index))<br> self.selectedDirectoryPath="/".join(dirHierarchy)<br>
print(self.selectedDirectoryPath)<br><br> #self.model.removeRow(0,index)<br><br> def addToList(self):<br> self.listWidget.addItem(self.selectedDirectoryPath)<br><br><br>if __name__ == "__main__":<br>
import sys<br> app = QtGui.QApplication(sys.argv)<br> ui = Ui_Dialog()<br> ui.show()<br> sys.exit(app.exec_())<br><br>See the screenshot also..<br><br><img title="?ui=2&view=att&th=12637fd44b798078&attid=0.1&disp=attd&realattid=ii_12637fd44b798078&zw" alt="?ui=2&view=att&th=12637fd44b798078&attid=0.1&disp=attd&realattid=ii_12637fd44b798078&zw" src="cid:ii_12637fd44b798078"><br>
<br>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)?<br><br>Any help would be much appreciated.. Thanks..<br>
<br>P.S: remove() and rmdir() is not recommended since it deletes the directories entirely from the file System!<br></span>