QStandardItemModelさて、いくつかの数字で満たされた本当に基本的な があります。なんとか表示できましたQTableView、大丈夫です。新しいモデル ( または のいずれかのサブクラス) を作成しましたQAbstractItemModel。QAbstractProxyModelこれは、既存のモデルのある種のレイヤーです。ソースモデルを設定する必要があり、この新しいレイヤーは実際のモデルに対して何らかの変換を行う必要があります。
私の問題は、最上位レイヤーで、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.horizontalHeader()->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() を返します。
}