0

I try to do something that can certainly be done easily but I can find a way.

I have a QtreeView displaying a QFileSystemModel that I custumized with two additionnal columns (sections). I work with directories named by date (YYYYMMDDHHMMSS+num). 'num' at the end of the string is a reference (an integer,ex: 04 or 12 or 53 ....) that can be similar to other directory name. 'num' is displayed in a fourth column.

I would like to group all directories with the similar references (for exemple in an ascending order) and also sort the directories by name (date) within each group.

Can you give me a hand please.

Folders looks like this:

201307
    2013072400000053
    2013072500000006
    2013072600000053
    2013072700000006
    2013072800000006
    2013072900000053
    2013073000000006
    2013073100000057
201308
    2013082400000006
    2013082500000053
    2013082600000053
    2013082700000057
    2013082800000006
    2013082900000057
    2013083000000006
    2013083100000053
    ...

code :

from PyQt4              import QtGui
from PyQt4              import QtCore
import sys

rootpathsource = " "

class MyFileSystemModel(QtGui.QFileSystemModel):

    def columnCount(self, parent = QtCore.QModelIndex()):
        # Add two additionnal columns for status and Instrument number 
        return super(MyFileSystemModel, self).columnCount() + 1

    def headerData(self, section, orientation, role):
        # Set up name for the two additionnal columns
        if section == 4 and role == QtCore.Qt.DisplayRole :
           return 'Number ref'
        else:
            return super(MyFileSystemModel, self).headerData(section, orientation, role)

    def data(self, index, role):      

        if index.column() == 4: #if ref
            ind          = index.parent()
            parentString =  ind.data().toString()
            if parentString in  self.fileInfo(index).fileName() and self.fileInfo(index).isDir() == True and role == QtCore.Qt.DisplayRole:
                return self.fileInfo(index).fileName()[-2:] # take the last two digits
        else:
            return super(MyFileSystemModel, self).data(index, role)

        if role == QtCore.Qt.TextAlignmentRole:
            return QtCore.Qt.AlignLeft

class TreeViewUi(QtGui.QWidget):

    def __init__(self, parent=None):
        super(TreeViewUi, self).__init__(parent)    

        self.model = MyFileSystemModel(self)
        self.model.setRootPath(rootpathsource)

        self.indexRoot = self.model.index(self.model.rootPath())

        self.treeView = QtGui.QTreeView(self)
        self.treeView.setExpandsOnDoubleClick(False)
        self.treeView.setModel(self.model)
        self.treeView.setRootIndex(self.indexRoot)
        self.treeView.setColumnWidth(0,300)

        self.layout = QtGui.QVBoxLayout(self)
        self.layout.addWidget(self.treeView)

class MainGui(QtGui.QMainWindow): 

    def __init__(self, parent=None):
        super(MainGui,self).__init__(parent)

        #QTreeView widget for files selection
        self.view = TreeViewUi()   
        self.setCentralWidget(self.view)
        self.resize(600,700)

def main():

    main = MainGui()
    main.show()    
    return main

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    StartApp = main()
    sys.exit(app.exec_())  
user3046026
  • 149
  • 1
  • 14

1 Answers1

1

The way to do this is to use the QSortFilterProxyModel class.

All you have to do is create a subclass of it and reimplement its lessThan method. This method has two QModelIndex arguments which can be compared in any way you like before returning either True or False. In your case, you would first check which column was being sorted, and just return the value of the base-class lessThan method for the "standard" columns. But for the "Number Ref" column, you would first compare numerically, and then if that was equal, compare the values of the "Name" column for the same row.

To install the sort-filter proxy, you just need to do:

    self.sortFilter = MyCustomSortFilterProxy(self)
    self.sortFilter.setSourceModel(self.model)
    self.treeView.setModel(self.sortFilter)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336