2

私には2つのモデルがあります:( MyModelinherits QAbstractItemModelそのツリー)とMyProxyModelinherits QSortFilterProxyModel)。

MyModelの列数は1で、MyModelの項目には、MyProxyModelを使用してQTableViewに表示する必要のある情報が含まれています。MyProxyModelを。で使用しましたMyProxyModel::columnCount() == 5

関数をオーバーロードしMyProxyModel::data()ました。ただし、テーブルビューには、列1(MyModel::columnCount)のデータのみが表示されます。

MyProxyModel::data()デバッグした後、でインデックスのみを取得することがわかりましたcolumn < MyModel::columnCount()(を使用MyModel::columnCount()して無視しているようですMyProxyModel::columnCount())。

テーブルビューのヘッダーセクションでは、カウントはMyProxyModel::columnCount()(OK;))に等しくなります。

セル内の情報を表示するにはどうすればよいcolumn > MyModel::columnCount()ですか?

MyModel.cpp:

int MyModel::columnCount(const QModelIndex& parent) const
{
    return 1;
}

MyProxyModel.cpp:

int MyProxyModel::columnCount(const QModelIndex& parent) const
{
    return 5;
}

QVariant MyProxyModel::data(const QModelIndex& index, int role) const
{
    const int  r = index.row(),
                  c = index.column();
    QModelIndex itemIndex = itemIndex = this->index(r, 0, index.parent());
    itemIndex = mapToSource(itemIndex);
    MyModel model = dynamic_cast<ItemsModel*>(sourceModel());
    Item* item = model->getItem(itemIndex);
    if(role == Qt::DisplayRole)
    {
          if(c == 0)
          {
                return model->data(itemIndex, role);
          }
          return item->infoForColumn(c);
    }
    return QSortFilterProxyModel::data(index, role)
}
4

1 に答える 1

1

Krzysztof Ciebieraが言ったように、それほど多くの言葉ではありません。あなたdatacolumnCountメソッドは正しく宣言されていないため、呼び出されることはありません。実装することになっている仮想メソッドには署名があります

int columnCount(const QModelIndex&) const;
QVariant data(const QModelIndex&, int) const;

メソッドのシグネチャは異なりますが

int columnCount(QModelIndex&) const;
QVariant data(QModelIndex&, int);

したがって、彼らは呼ばれません。メソッドがモデルインデックスへの非定数参照を誤って予期していることに注意してください。このメソッドdata()は、非定数オブジェクトインスタンスも想定しています。

于 2012-06-26T23:39:00.010 に答える