我想從ListView
中選擇多個項目。在C++
我會做這樣的事情從ListView中選擇多個項目
if (clicked_card->is_selected) {
clicked_card->is_selected = false;
int i = 0;
while(selected_cards[i] != clicked_card) i++;
selected_cards.erase(selected_cards.begin() + i);
} else {
clicked_card->is_selected = true;
selected_cards.push_back(clicked_card);
}
上面的代碼使用指針進行比較。那麼如何在QML
這樣的選擇。我已經想出瞭解決的辦法是這樣的
Card.qml
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});
}
}
}
}
GameScene.qml
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 < gameScene.selectedCards.length; i += 1) {
if (gameScene.selectedCards[i].selectSeq == userData) {
gameScene.selectedCards.splice(i, 1);
break;
}
}
}
}
的與上面的代碼的問題是,我將財產isSelected
存儲在由系統創建和銷燬的委託中。所以這給了我一個錯誤的解決方案。在解決方案中是否有更好的多重選擇或改進方法?我使用QAbstractListModel
來自C++的model
。