2017-03-17 1 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によれば、itemDelegateの現在のインデックスはstyleData.indexで取得できます。予想通り

これは動作するはずです:

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) 
     } 
    } 
} 
関連する問題