2016-09-14 42 views
0

我使用QSqlTableModel在視圖中顯示SQL表。使用代理將虛擬列添加到Qt SQL模型

我想顯示基於該行數據的附加狀態列中,我使用自定義QIdentityProxyModel,我增加信息columnCount並返回數據對於不中存在新的虛擬列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

嘗試實現'columnCount'到'返回QIdentityProxyModel :: columnCount()+ 1;',這樣你就可以避免錯誤的值分配給你的'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'或任何接收行整數/列整數的方法,它將直接調用'sourceModel()'等價方法,而不先調用'mapToSource ',例如。在你的情況下,它工作正常,因爲你的額外列是最後一個,但在我的情況下,我的虛擬列是第一個,我不得不重新實現幾乎所有的方法。以及如何知道您是否想爲您的模型使用持久性索引。 –

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;'工作正常,thx!這仍然不是我的問題,我仍然正確更新m_mySqlTableColumnCount。 – ymoreau