pyqt4 - Sorting by column -
i try can done can find way.
i have qtreeview displaying qfilesystemmodel custumized 2 additionnal columns (sections). work directories named date (yyyymmddhhmmss+num). 'num' @ end of string reference (an integer,ex: 04 or 12 or 53 ....) can similar other directory name. 'num' displayed in fourth column.
i group directories similar references (for exemple in ascending order) , sort directories name (date) within each group.
can give me hand please.
folders looks this:
201307 2013072400000053 2013072500000006 2013072600000053 2013072700000006 2013072800000006 2013072900000053 2013073000000006 2013073100000057 201308 2013082400000006 2013082500000053 2013082600000053 2013082700000057 2013082800000006 2013082900000057 2013083000000006 2013083100000053 ... code :
from pyqt4 import qtgui pyqt4 import qtcore import sys rootpathsource = " " class myfilesystemmodel(qtgui.qfilesystemmodel): def columncount(self, parent = qtcore.qmodelindex()): # add 2 additionnal columns status , instrument number return super(myfilesystemmodel, self).columncount() + 1 def headerdata(self, section, orientation, role): # set name 2 additionnal columns if section == 4 , 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() , self.fileinfo(index).isdir() == true , role == qtcore.qt.displayrole: return self.fileinfo(index).filename()[-2:] # take last 2 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 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_())
the way use qsortfilterproxymodel class.
all have create subclass of , reimplement lessthan method. method has 2 qmodelindex arguments can compared in way before returning either true or false. in case, first check column being sorted, , return value of base-class lessthan method "standard" columns. "number ref" column, first compare numerically, , if equal, compare values of "name" column same row.
to install sort-filter proxy, need do:
self.sortfilter = mycustomsortfilterproxy(self) self.sortfilter.setsourcemodel(self.model) self.treeview.setmodel(self.sortfilter)
Comments
Post a Comment