Tarod
Tarod

Reputation: 7170

Get current index model in itemDelegate

I'm working with this code and I'm trying to get the number of rows or if the selected item has or not children. But I'm getting a weird behavior.

I added the next code to the 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))
            }
        }
    }

And my model (treemodel.cpp) has the following method:

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;
}

When I click on the checkboxes, sometimes the number of rows is right but the most of times is wrong. I.e. rowCount is not returning 0 when I click a row without children or 6 when we only have 4 children.

rowCount works fine. It returns always the right value when we're expanding the tree using the arrows so I suppose the problem is how I'm passing the index to the print method.

Upvotes: 1

Views: 1363

Answers (1)

Blabdouze
Blabdouze

Reputation: 1121

According to the documentation you can retrieve the current index in the itemDelegate with styleData.index.

This should work as expected :

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)
        }
    }
}

Upvotes: 2

Related Questions