2017-06-13 31 views
0

我有一個樹模型類繼承QAbstractItemModel(比方說Model)和兩個過濾代理模型繼承QSortFilterProxyModel。這兩種代理模式都在同一個模型實例設置:Qt模型/視圖:代理模型上的setRootIndex失敗

m_groupFilter->setSourceModel(m_model); 
m_parameterFilter->setSourceModel(m_model); 

和這些過濾器是用戶界面上的兩個QTreeView S設定:

ui->treeViewGroups->setModel(m_groupFilter); 
ui->treeViewParameters->setModel(m_parameterFilter); 

現在,我想第二個觀點setRootIndex(),到從第一個選擇的項目。連接信號等我這樣做:

QModelIndex actualIndex = m_groupFilter->mapToSource(index); 
QModelIndex mappedIndex = m_parameterFilter->mapFromSource(actualIndex); 
qDebug() << mappedIndex.isValid(); 
qDebug() << ui->treeViewParameters->model(); 
qDebug() << mappedIndex.model(); 
ui->treeViewParameters->setRootIndex(mappedIndex); 

以上代碼失敗,此輸出,並警告:

true 
ConfigurationParameterFilterModel(0x43d190) 
ConfigurationParameterFilterModel(0x43d190) 
QAbstractItemView::setRootIndex failed : index must be from the currently set model 
QAbstractItemView::setRootIndex failed : index must be from the currently set model 

我可以看到,這隻有當發生輸入指數模型setRootIndex和對象調用setRootIndex,不同(這裏不是這種情況)或者索引是無效的(這裏既不是這種情況)。以下是Qt source code that generates the warning

void QAbstractItemView::setRootIndex(const QModelIndex &index) 
{ 
    Q_D(QAbstractItemView); 
    if (Q_UNLIKELY(index.isValid() && index.model() != d->model)) { 
     qWarning("QAbstractItemView::setRootIndex failed : index must be from the currently set model"); 
     return; 
    } 
    d->root = index; 
    d->doDelayedItemsLayout(); 
    d->updateGeometry(); 
} 

那麼,爲什麼它不工作?

回答

0

您顯示的代碼是好的。我將它複製到Qt Creator的標準模板項目中。它工作正常。請檢查您的代理模型實施和setRootIndex調用的其他地方。