2017-03-17 29 views
0

我正在與this code一起工作,我正在嘗試獲取行數或所選項目是否有孩子。但我得到一個奇怪的行爲。獲取itemDelegate中的當前索引模型

我加入了一個代碼到QML:

itemDelegate: Item { 
     CheckBox { 
      id: checkbox 
      text: styleData.value.text 
      checked:false 
      visible: styleData.value === undefined ? false : true 

      onClicked: { 
       theModel.print(styleData.row, styleData.column, 
           theModel.index) 
       theModel.print(styleData.row, styleData.column, 
           theModel.index(styleData.row, 
               styleData.column, 
               theModel.currentIndex)) 
      } 
     } 
    } 

我的模型(treemodel.cpp)具有以下方法:

bool TreeModel::print(int row, int column, const QModelIndex &modelIndex) 
{ 
    createIndex(row, column, 1); 

    qDebug() << Q_FUNC_INFO 
      << " row: " << row 
      << " column: " << column 
      << " rowCount (a): " << this->rowCount(index(row, column, modelIndex)) 
      << " rowCount (b): " << this->rowCount(modelIndex) 
      << " hasChildren (a): " << this->hasChildren(index(row, column, modelIndex)) 
      << " hasChildren (b): " << this->hasChildren(modelIndex); 

    return true; 
} 

當我點擊複選框,有時數的行是正確的,但大部分時間都是錯誤的。即rowCount當我點擊沒有孩子的行時沒有返回0,或當我們只有4個孩子時沒有返回6。

rowCount工作正常。當我們使用箭頭擴展樹時,它總是返回正確的值,所以我想問題是我如何將索引傳遞給print方法。

+0

如果'theModel'是'TreeModel'的'theModel.index'和'theModel.currentIndex'不存在。你所擁有的只是一個'TreeModel :: index()',但它不暴露給QML。 –

回答

1

根據documentation,您可以通過styleData.index檢索itemDelegate中的當前索引。

這應該按預期工作:

itemDelegate: Item { 
    CheckBox { 
     id: checkbox 
     text: styleData.value.text 
     checked:false 
     visible: styleData.value === undefined ? false : true 

     onClicked: { 
      theModel.print(styleData.row, styleData.column, 
          styleData.index) 
     } 
    } 
} 
相關問題