4

如何在 QML ListView 中选择一些元素并将其索引发送到 C++ 代码?

4

4 回答 4

5

做这样的事情:如果一个元素被点击,设置它的属性选择(或者你怎么称呼它),并在委托中设置如果选择为真,那么它的格式应该不同。加上将它添加到一些列表中,以使用它。

于 2013-02-01T15:43:50.317 回答
3

我很确定没有办法让 QML ListView 多选。Qt Declarative 专注于触摸屏的使用,在纯触摸 UI 中没有有意义的多选方式。

于 2010-10-07T09:07:15.733 回答
2

我有同样的问题,我发现实现它的最佳方法是为列表视图创建一个新角色。让我们假设它是名字并被选中。您需要同时使用 onCurrentIndexChanged 和 onClicked,因为如果您滚动,这将更改项目但它不是点击。在他们两个中将选择的角色更改为 true,或者根据您的需要进行调整,可能您不需要滚动来选择,因此只使用 onClicked。单击后,您可以将选择的角色更改为 true

onCurrentIndexChanged:
{
mListModel.append({"firstName": newEntry,"selected":true})
}

onClicked:
{
mListModel.append({"firstName": newEntry,"selected":true})
}

那么您可以在 deligate 中使用高亮,这将根据所选状态更改颜色。

这是经过测试可以工作的完整代码

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


}
于 2019-11-14T19:39:29.603 回答
1

您可以尝试在奇数单击时获取 ListItem 的数据并将其存储到数组中,并在偶数单击时从数组中删除 ListItem 的数据。可能是一个简单的锻炼,而不是像项目一样创建一个复选框列表。

于 2011-11-24T18:13:05.370 回答