QML ListViewでいくつかの要素を選択し、そのインデックスをC ++コードに送信するにはどうすればよいですか?
4 に答える
次のようなことを行います。要素がクリックされた場合は、そのプロパティを選択して設定し(または、どのように呼び出しても)、選択した場合は別の形式にする必要があることをデリゲートに設定します。さらに、それをいくつかのリストに追加して、それを操作します。
QMLListViewを複数選択可能にする方法はないと確信しています。Qt Declarativeはタッチスクリーンの使用に焦点を合わせており、純粋なタッチUIで複数選択する意味のある方法はありません。
私は同じ問題を抱えていましたが、それを実装するための最良の方法は、リストビューに新しい役割を作成することです。それが名で選択されていると仮定しましょう。onCurrentIndexChangedとonClickedの両方を使用する必要があります。これは、スクロールするとアイテムが変更されますが、クリックではないためです。どちらの場合も、選択した役割をtrueに変更するか、必要に応じて調整します。スクロールする必要がないため、onClickedのみを使用できます。クリックすると、選択した役割をtrueに変更できます
onCurrentIndexChanged:
{
mListModel.append({"firstName": newEntry,"selected":true})
}
と
onClicked:
{
mListModel.append({"firstName": newEntry,"selected":true})
}
次に、デリゲートでハイライトを使用できます。これにより、選択した状態に基づいて色が変更されます。
これが動作するようにテストされた完全なコードです
//copyright: Dr. Sherif Omran
//licence: LPGL (not for commercial use)
import QtQuick 2.12
import QtQuick.Layouts 1.12
Item {
property string addnewitem:""
property int removeitemindex: -1
property string appenditemstring: ""
property int appenditemindx:-1
property int fontpoint: 20
property int radiuspoint: 14
property int spacingvalue: 0
property string delegate_color:"beige"
property string delegate_border_color:"yellowgreen"
property string highlight_color:"deeppink"
signal selectedvalueSignal (string iTemstring, string stateval)
property string sv: ""
property int indexcopy:0
id:lstmodelitem
width: parent.width
height: parent.height
ListModel {
id : mListModel
// ListElement {
// firstName : "John"
// }
}
ColumnLayout {
anchors.fill: parent
ListView{
id : mListViewId
model:mListModel
delegate :delegateId
Layout.fillWidth : true
Layout.fillHeight: true
clip: true
snapMode: ListView.SnapToItem //this stops the view at the boundary
spacing: spacingvalue
highlight: Rectangle
{
id: highlightid
width: parent.width
color: mListModel.selected==="true"?"blue":highlight_color
border.color: "beige"
z:3
opacity: 0.2
}
highlightRangeMode: ListView.StrictlyEnforceRange
highlightFollowsCurrentItem:true
onCurrentIndexChanged:
{
console.log("olistdynamic Indexchanged" + currentIndex)
mListViewId.currentIndex=currentIndex
lstmodelitem.selectedvalueSignal(currentIndex, mListModel.selected)
indexcopy=currentIndex
}
}
}
function getindex()
{
console.log("current index = " + indexcopy)
return mListViewId.currentIndex
}
function setindex(index)
{
//console.log("olistdynamic set index"+index)
mListViewId.currentIndex=index
}
function add2Item(newEntry,statev){
console.log("added item with value = " + newEntry + "state " + statev)
mListModel.append({"firstName": newEntry,"selected":statev})
}
function deleteItem(index){
mListModel.remove(index,1)
}
function appendIdem(index,valueEntry,newselectedsate)
{
console.log("append item")
mListModel.set(index,{"firstName": valueEntry,"selected":newselectedsate})
}
Component {
id : delegateId
Rectangle {
id : rectangleId
width : parent.width // Remember to specify these sizes or you'll have problems
height: textId.implicitHeight*1.2
color: selected==="true"?"blue":delegate_color
border.color: delegate_border_color
radius: radiuspoint
Text {
id : textId
anchors.centerIn: parent
text : firstName
font.pointSize: fontpoint
}
MouseArea {
anchors.fill: parent
onClicked: {
lstmodelitem.selectedvalueSignal(mListModel.firstName,mListModel.selected)
mListViewId.currentIndex=index
console.log("current index = " + index)
indexcopy=index
appendIdem(index,firstName,"true")
}
onClipChanged:
{
//console.log("a")
}
}
}
}
//if the item has been changed from null to text
onAddnewitemChanged: {
console.log("added item" + addnewitem)
add2Item(addnewitem)
}
//remove item with index
onRemoveitemindexChanged: {
console.log("remove item")
deleteItem(removeitemindex)
}
//to change the item, change the index first then the string
onAppenditemstringChanged: {
appendIdem(appenditemindx,appenditemstring)
}
}
ListItemのデータを取得して、奇数クリックで配列に格納し、偶数クリックで配列からListItemのデータを削除しようとする場合があります。アイテムのようなチェックボックスのリストを作成する代わりに、単純なトレーニングである可能性があります。