我尝试做一些当然可以轻松完成的事情,但我可以找到方法。
我有一个 QtreeView 显示一个 QFileSystemModel,我用两个附加列(部分)进行了自定义。我使用按日期命名的目录 (YYYYMMDDHHMMSS+num)。字符串末尾的“num”是一个引用(整数,例如:04 或 12 或 53 ....),可以与其他目录名称类似。'num' 显示在第四列中。
我想对具有相似引用的所有目录进行分组(例如按升序排列),并按每个组中的名称(日期)对目录进行排序。
你能帮我一把吗?
文件夹如下所示:
201307
2013072400000053
2013072500000006
2013072600000053
2013072700000006
2013072800000006
2013072900000053
2013073000000006
2013073100000057
201308
2013082400000006
2013082500000053
2013082600000053
2013082700000057
2013082800000006
2013082900000057
2013083000000006
2013083100000053
...
代码 :
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_())