0

PySide を使用してプラグイン ブラウザーを作成しています。利用可能なプラグインは、次のような 3 次元モデルに格納されます。

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 次元モデルに変換する必要がありますか、それとももっと良い方法がありますか? カスタム ビューを標準のツリー ビューと同期できるようにしたいので、多次元モデルを選択しました。

ありがとう、フランク

編集1:これは、単純化された例として私が持っているものです。これがやり方(モデル構造を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 に答える