2016-09-14 12 views
0

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!

+0

'm_mySqlTableColumnCount'の値は何ですか? – Mike

+0

'QIdentityProxyModel :: columnCount()+ 1;'を返すために 'columnCount'を実装しようとすると、間違った値を'​​ m_mySqlTableColumnCount'に代入するのを避けることができます。 – Mike

+0

@Mikeこれは私が最初にやったことですが、何らかの理由で私のアプリが 'columnCount'を無限に呼び出してクラッシュしました。私は 'sourceModelChanged'シグナルを使って自分の値が正しいことを確認しました。値をチェックしても問題ありません。私のカラムカウントは良い値を返します。 – ymoreau

答えて

0

ビューは、仮想列のQModelIndexオブジェクトを取得する必要があるので、私はまた、上書きするために必要なプロキシでindex機能:

QModelIndex MyProxyModel::index(int row, int column, const QModelIndex &parent) const 
{ 
    if (column == columnCount()-1) 
     return createIndex(row, column); 

    return QIdentityProxyModel::index(row, column); 
} 

私だけ(データベースから)のテーブルを持っているので、私はそれができるのか分からないけれども、私は、親を気にしませんでしたcreateIndexは親を指定できないため、必要に応じて処理します。

+1

アイデンティティ・プロキシ・モデルの問題点は、列と行の数が基礎となるモデルと同じであることを前提としています。たとえば、 'insertColumn'や' sibling'や行/列の整数を受け取るメソッドを再実装しないと、最初に 'mapToSource 'を呼び出さずに' sourceModel() '同等のメソッドを直接呼び出します。例えば、あなたのケースでは、あなたの余分な列が最後のものなので、それは正常に動作しますが、私の場合、私の仮想列が最初のものであり、私はほとんどすべてのメソッドを再実装しなければなりませんでした。また、モデルに永続インデックスを使用するかどうかをどのように知っていますか。 –

1

m_mySqlTableColumnCountメンバーは不要です。列数を更新するソースモデルの信号を聞いて、常に正しいことを確認する必要があります。悲しいかな、それは不要です。

int MyProxyModel::columnCount(const QModelIndex& parent) const 
{ 
    return sourceModel() ? (QIdentityProxyModel::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 
{ 
    if (proxyIndex.column() == columnCount()-1) { 
     qDebug() << proxyIndex.row() << proxyIndex.column(); 
     ... 
    } 
    return QIdentityProxyModel::data(proxyIndex, role); 
} 
+0

私はこれを最初に試みましたが、それは 'columnCount()'への膨大なスタック呼び出しでクラッシュします。なぜ私は見つからなかったのですが、 'return sourceModel()? (sourceModel() - > columnCount()+ 1):0; 'うまく動作します。それでも私の問題はここではない、私はm_mySqlTableColumnCountを正しく更新していた。 – ymoreau

関連する問題