Search code examples
pythonpyqt4qtreeview

filtering in qtreeview in pyqt and to display only folders


I'm working on a GUI which needs qtreeview in the layout. now I've written a sample code to get know with the working of qtreeview and I got stuck with a problem.

My problems are: 1. it should display only folders present in the given path.

2.doubleclicking the folder in the qtreeview should give contents of the folder in the column view with owner also as one of the column(here I mean to say if I do a "ll" in terminal I get a list with owners of the folder) I want that information as well.

this is my code:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(1150, 905)
        self.gridLayout_2 = QtGui.QGridLayout(Dialog)
        self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
        self.groupBox = QtGui.QGroupBox(Dialog)
        self.groupBox.setObjectName(_fromUtf8("groupBox"))
        self.gridLayout = QtGui.QGridLayout(self.groupBox)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.treeView = QtGui.QTreeView(self.groupBox)
        self.treeView.setObjectName(_fromUtf8("treeView"))
        self.gridLayout.addWidget(self.treeView, 0, 0, 1, 1)
        self.columnView = QtGui.QColumnView(self.groupBox)
        self.columnView.setObjectName(_fromUtf8("columnView"))
        self.gridLayout.addWidget(self.columnView, 0, 1, 1, 1)
        self.gridLayout_2.addWidget(self.groupBox, 0, 0, 1, 2)
        spacerItem = QtGui.QSpacerItem(1073, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.gridLayout_2.addItem(spacerItem, 1, 0, 1, 1)
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.gridLayout_2.addWidget(self.pushButton, 1, 1, 1, 1)
        self.fileSystemModel = QtGui.QFileSystemModel(self.treeView)
        self.fileSystemModel.setFilter(QtCore.QDir.AllDirs | QtCore.QDir.NoDotAndDotDot | QtCore.QDir.AllEntries)
        self.fileSystemModel.setReadOnly(False)
        self.root = self.fileSystemModel.setRootPath('/home/hamanda/present_wrkng_python')
        self.treeView.setModel(self.fileSystemModel)
        self.treeView.setRootIndex(self.root)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.groupBox.setTitle(_translate("Dialog", "List of folders", None))
        self.pushButton.setText(_translate("Dialog", "Quit", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

Please help me with this I'm new to pyqt programming


Solution

  • To get directories only:

    self.filemodel.setFilter(QtCore.QDir.AllDirs|QtCore.QDir.NoDotAndDotDot)
    

    to have a double-click in the tree reset the column view:

    ui.treeView.doubleClicked.connect(ui.columnView.setRootIndex)
    

    The column view is not the one you want to display details, however; the treeView or listView would be the ones to use. As you can see, the treeView already gives some details by default. I don't know how to get the owner via this QFileSystemModel, you would probably have to subclass.

    To display files on the one side and only folders on the other, you would either need two models, a proxy model, or a custom model.