QSqlTableModelを使用してビューにSQLテーブルを表示します。プロキシを使用してQt SQLモデルに仮想列を追加します。
私はcolumnCountのを高め、中に存在しない新しい仮想列にデータを返すところそれは私がQIdentityProxyModelカスタムを使用するために、私は、行データに基づいて、追加のステータス列を表示したいですQSqlTableModel。
int MyProxyModel::columnCount(const QModelIndex& parent) const
{
return sourceModel() ? (sourceModel()->columnCount() + 1) : 0;
}
QVariant MyProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (section == columnCount()-1 &&
orientation == Qt::Horizontal &&
role == Qt::DisplayRole)
{
return tr("MyHeader");
}
return QIdentityProxyModel::headerData(section, orientation, role);
}
QVariant MyProxyModel::data(const QModelIndex &proxyIndex, int role) const
{
qDebug() << "data " << proxyIndex.row() << proxyIndex.column();
// ...never called for my extra column (== columnCount()-1)
if (proxyIndex.column() == columnCount()-1 && role == Qt::DisplayRole)
return QString("I am virtual");
return QIdentityProxyModel::data(proxyIndex, role);
}
編集:私はコメントにに関してより簡単な何かのためにコードを変更しました。私はまだ同じ問題があります。
私の問題は、ビューは私の仮想列のデータを要求することはありません、それは(データを呼び出すことである)他のすべての実際のSQLテーブルの列ではなく、最後の仮想いずれかの、私が何を見逃していますか? また、ヘッダーデータは余分な列でうまく機能していますが、問題はデータのみにあります。ビューは追加の列を描画しますが、内容は空です(行の背景が交互に塗られていなくても)。 Thx!
'm_mySqlTableColumnCount'の値は何ですか? – Mike
'QIdentityProxyModel :: columnCount()+ 1;'を返すために 'columnCount'を実装しようとすると、間違った値を' m_mySqlTableColumnCount'に代入するのを避けることができます。 – Mike
@Mikeこれは私が最初にやったことですが、何らかの理由で私のアプリが 'columnCount'を無限に呼び出してクラッシュしました。私は 'sourceModelChanged'シグナルを使って自分の値が正しいことを確認しました。値をチェックしても問題ありません。私のカラムカウントは良い値を返します。 – ymoreau