5

QStandardItemModelさて、いくつかの数字で満たされた本当に基本的な があります。なんとか表示できましたQTableView、大丈夫です。新しいモデル ( または のいずれかのサブクラス) を作成しましたQAbstractItemModelQAbstractProxyModelこれは、既存のモデルのある種のレイヤーです。ソースモデルを設定する必要があり、この新しいレイヤーは実際のモデルに対して何らかの変換を行う必要があります。

私の問題は、最上位レイヤーで、data( const QModelIndex & index, int role )メンバー関数が呼び出されなかった「レイヤーモデル」と言いますが、ロールパラメーターで表示方法を変更したいということです。

data(index,role)元のモデルは常に呼び出され、レイヤー モデルは呼び出されないことを示すサンプル コードを次に示しdata(index,role)ます。なんで?QTableView オブジェクトはどのようにして最上位レイヤーを「スキップ」できますdata(index,role)か?

#include <QtGui/QApplication>
#include <QtGui>
#include <QStandardItemModel>

クラス MyModel : public QStandardItemModel
{
公衆:
    MyModel(const int r, const int c, QObject* 親 = 0) : QStandardItemModel(r,c,parent) {}
    QVariant データ ( const QModelIndex & index, int role = Qt::DisplayRole ) const {
        qDebug() << "mymodel データ";
        return this->itemFromIndex(index)->data(role);
    }
};

クラス MyProxyModel : public QAbstractProxyModel
{
公衆:

    MyProxyModel(QObject* 親 = 0) : QAbstractProxyModel(親) {}
    QModelIndex インデックス ( int 行、int 列、const QModelIndex & 親 = QModelIndex() ) const {
        return this->sourceModel()->index(row,column,parent);
    }
    QModelIndex 親 ( const QModelIndex & インデックス ) const {
        return this->sourceModel()->parent(index);
    }
    QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const
    {
        sourceIndex を返します。
    }
    QModelIndex mapToSource ( const QModelIndex & proxyIndex ) const
    {
        proxyIndex を返します。
    }
    QVariant データ ( const QModelIndex & index, int role = Qt::DisplayRole ) const {
        qDebug() << "myproxymodel データ";
        return this->sourceModel()->data(index,role);
    }

    int rowCount ( const QModelIndex & 親 = QModelIndex() ) const {
        return this->sourceModel()->rowCount(parent);
    }
    int columnCount ( const QModelIndex & 親 = QModelIndex() ) const {
        これを返します->sourceModel()->columnCount(親);
    }
};

int main(int argc, char *argv[])
{
    QApplication app(argc,argv);
    MyModel モデル(8, 2);
    MyProxyModel マイモデル;
    mymodel.setSourceModel(&モデル);

    QTableView tableView;
    tableView.setModel(&mymodel);

    tableView.horizo​​ntalHeader()->setStretchLastSection(true);
    for (int 行 = 0; 行 < 8; ++行) {
        for (int 列 = 0; 列 < 2; ++列) {
            QModelIndex index = model.index(row, column, QModelIndex());
            model.setData(index, QVariant((row+1) * (column+1)));
        }

    }
    tableView.show();
    app.exec() を返します。
}

4

1 に答える 1

6

QTableView はモデル インデックスを使用してデータを取得するため、おそらくこのようなものです。

QModelIndex index = model->index(row, column, parentIndex); 
index.data(Qt::DisplayRole);

また、プロキシ モデルへのインデックスではなく、ソース モデルのモデル インデックスを返しています。

QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const {
    return this->sourceModel()->index(row,column,parent);
}

モデル インデックスをプロキシ モデルのインデックスに変換してみてください

QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const {
    return this->createIndex(row,column,row);
}

map to source 関数と map from source 関数を書き直すことを忘れないでください。


解決

class MyTableProxyModel : public QAbstractProxyModel
{
    Q_OBJECT
public:
    MyTableProxyModel (QObject* parent = 0) 
        : QAbstractProxyModel(parent) {
    }

    QModelIndex index(int row, int column, const QModelIndex& parent=QModelIndex()) const {
        return createIndex(row,column,row);
    }

    QModelIndex parent(const QModelIndex &index) const {
        //Works only for non-tree models
        return QModelIndex();
    }

    QModelIndex mapFromSource(const QModelIndex &source) const {
        return index(source.row(), source.column(), source.parent());
    }

    QModelIndex mapToSource(const QModelIndex &proxy) const {
        return (sourceModel()&&proxy.isValid())
            ? sourceModel()->index(proxy.row(), proxy.column(), proxy.parent()) 
            : QModelIndex();
    }

    QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const {
        qDebug() << "myproxymodel data";
        return mapToSource(index).data(role);
    }

    int rowCount ( const QModelIndex & parent = QModelIndex() ) const {
        return sourceModel() ? sourceModel()->rowCount(parent) : 0;
    }

    int columnCount ( const QModelIndex & parent = QModelIndex() ) const {
        return sourceModel() ? sourceModel()->columnCount(parent) : 0;
    }
};
于 2009-09-07T07:21:43.850 に答える