0

我正在使用 PySide 编写插件浏览器。可用的插件存储在一个三维模型中,如下所示:

pluginType/pluginCategory/pluginName

例如:

python/categoryA/toolA
python/categoryB/toolAA

等等

在我的自定义视图中,我在列表中显示给定插件类型(即“python”)的所有工具,无论它们的类别如何:

(python)
categoryA/toolA
categoryA/toolB
categoryA/toolC
categoryB/toolAA
categoryB/toolBB
categoryB/toolCC

我现在想知道如何最好地对该视图进行排序,因此无论其父类别如何,这些工具都按名称排序。我当前的代理模型中的排序方法会为每个类别生成一个排序列表,就像上面的一样,但我所追求的是:

(python)
categoryA/toolA
categoryB/toolAA
categoryA/toolB
categoryB/toolBB
categoryA/toolC
categoryB/toolCC

我是否必须让我的代理模型将多维源模型转换为一维模型才能实现这一点,还是有更好的方法?我希望能够将自定义视图与标准树视图同步,这就是我选择多维模型的原因。

谢谢,坦率

编辑1:这是我作为简化示例的内容。我不确定这是否是解决方法(将模型结构更改为一维模型),如果是,我不确定如何在代理模型中正确创建数据,所以它是按预期与源模型链接。

import sys
from PySide.QtGui import *
from PySide.QtCore import *

class ToolModel(QStandardItemModel):
    '''multi dimensional model'''
    def __init__(self, parent=None):
        super(ToolModel, self).__init__(parent)
        self.setTools()

    def setTools(self):
        for contRow, container in enumerate(['plugins', 'python', 'misc']):
            contItem = QStandardItem(container)
            self.setItem(contRow, 0, contItem)
            for catRow, category in enumerate(['catA', 'catB', 'catC']):
                catItem = QStandardItem(category)
                contItem.setChild(catRow, catItem)
                for toolRow, tool in enumerate(['toolA', 'toolB', 'toolC']):
                    toolItem = QStandardItem(tool)
                    catItem.setChild(toolRow, toolItem)

class ToolProxyModel(QSortFilterProxyModel):
    '''
    proxy model for sorting and filtering.
    need to be able to sort by toolName regardless of category,
    So I might have to convert the data from sourceModel to a 1-dimensional model?!
    Not sure how to do this properly.
    '''
    def __init__(self, parent=None):
        super(ToolProxyModel, self).__init__(parent)

    def setSourceModel(self, model):
        index = 0
        for contRow in xrange(model.rowCount()):
            containerItem = model.item(contRow, 0)
            for catRow in xrange(containerItem.rowCount()):
                categoryItem = containerItem.child(catRow)
                for itemRow in xrange(categoryItem.rowCount()):
                    toolItem = categoryItem.child(itemRow)
                    # how to create new, 1-dimensional data for self?



app = QApplication(sys.argv)
mainWindow = QWidget()
mainWindow.setLayout(QHBoxLayout())

model = ToolModel()
proxyModel = ToolProxyModel()
proxyModel.setSourceModel(model)
treeView = QTreeView()
treeView.setModel(model)
treeView.expandAll()
listView = QListView()

listView.setModel(proxyModel)

mainWindow.layout().addWidget(treeView)
mainWindow.layout().addWidget(listView)
mainWindow.show()
sys.exit(app.exec_())

编辑:或者我应该问如何最好地准备一个源模型,以便 QTreeView 可以使用它,但也可以按上述方式排序以在列表视图中显示?!

4

1 回答 1

0

使用 QTableView 并按 tool_name 列排序(使用排序代理)。

于 2012-07-24T13:30:13.047 回答