Multiple items selection from ListView
<p>I want to select multiple items from a <code>ListView</code>. In <code>C++</code> I would have done something like this</p>

<pre><code>if (clicked_card-&gt;is_selected) {
    clicked_card-&gt;is_selected = false;
    int i = 0;
    while(selected_cards[i] != clicked_card) i++;
    selected_cards.erase(selected_cards.begin() + i);
} else {
    clicked_card-&gt;is_selected = true;
    selected_cards.push_back(clicked_card);
}
</code></pre>

<p>The above code uses pointer for comparison. So how to so such selection in <code>QML</code>. The solution I've come up with is something like this</p>

<p><strong>Card.qml</strong></p>

<pre><code>Image {
    id: delegate
    property bool isSelected: false
    ...
    MouseArea {
        onClicked: {
            if(isSelected === true) {
                isSelected = false;
                gameScene.deselectCard(selectSeq);
            }
            else {
                isSelected = true;
                gameScene.selectCard({'opParam': opParam, 'selectSeq': selectSeq});
            }
        }
    }
}
</code></pre>

<p><strong>GameScene.qml</strong></p>

<pre><code>Item {
    id: gameScene
    property var selectedCards: []

    signal selectCard(variant userData)
    onSelectCard: {
        gameScene.selectedCards.push(userData)
    }

    signal deselectCard(variant userData)
    onDeselectCard: {
        for (var i = 0; i &lt; gameScene.selectedCards.length; i += 1) {
            if (gameScene.selectedCards[i].selectSeq == userData) {
                gameScene.selectedCards.splice(i, 1);
                break;
            }
        }
    }
}
</code></pre>

<p>The problem with the above code is that I'm storing property <code>isSelected</code> in a delegate which is created and destroyed by the system. So this is giving me false solution. Is there any better way of multiple selection or any improvements in the solution ? I'm using <code>model</code> from C++ by subclassing <code>QAbstractListModel</code>.</p>