如何将搜索过滤器添加到ComboBox

时间:2016-10-24 06:11:22

标签: qt qml qtquickcontrols2

我在QML中有一个ComboBox,它拥有超过2000个项目。滚动到最后一个元素的项目太多。如何为ComboBox添加添加搜索过滤器?当我输入字母时,匹配的结果应显示在列表中。或者,我可以提高鼠标滚轮速度以使滚动更快吗?

我发现了解决方案:

    ComboBox {
                id:trCombo
                model:combotr.datalist
                textRole: "value"
                anchors.fill: parent
                currentIndex:-1;



                contentItem: Text {
                    leftPadding: 0
                    rightPadding: trCombo.indicator.width + trCombo.spacing

                    text:trCombo.currentIndex==-1 ? "":trCombo.model[trCombo.currentIndex].value
                    font: trCombo.font
                    horizontalAlignment: Text.AlignLeft
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideRight
                }

                popup: T.Popup {
                    id:mpopup
                    y: trCombo.height - (trCombo.visualFocus ? 0 : 1)

                    width: trCombo.width
                    implicitHeight: listview.contentHeight
                    topMargin: 6
                    bottomMargin: 6

                // focus: true
                    closePolicy: Popup.NoAutoClose

                    contentItem: Item {


                        Column
                        {
                        anchors.fill: parent
                        spacing: 5
                        TextField
                        {

                        placeholderText: "arama yapın"
                        width: trCombo.width
                        height: dp(35)
                    // color: "red"
                        focus:true
                        inputMethodHints: Qt.ImhNoPredictiveText;

                        onTextChanged:{
                            //console.log("degisiyor");
                            process.filtertr(text);
                        }

                        onAccepted:{
                        //  console.log("Tasarım Bitti");
                            isfinished(true);
                        //  text="";


                        }




                        }

                        ListView {
                        id: listview
                        clip: true
                        model: trCombo.popup.visible ? trCombo.delegateModel : null
                        currentIndex: trCombo.highlightedIndex
                        width: trCombo.width
                        height:dp(500)


                        Rectangle {



                            z: 10
                            parent: listview
                            width: listview.width
                            height: listview.height
                            color: "transparent"
                            border.color: "#bdbebf"
                            layer.smooth: true
                        }

                        ScrollIndicator.vertical: ScrollIndicator { }
                    }

                    }
                }

                    background: Rectangle {  }

                    onClosed: {
                    if(!flag)
                    {
                        mpopup.open();
                    }

                    else
                    flag=false;
                    }


                }


                delegate: ItemDelegate {
                    width: trCombo.width
                    text: trCombo.textRole ? (Array.isArray(trCombo.model) ? modelData[trCombo.textRole] : model[trCombo.textRole]) : modelData
                    font.weight: trCombo.currentIndex === index ? Font.DemiBold : Font.Normal
                    highlighted: trCombo.highlightedIndex == index

                    onClicked: {
                        isfinished(true);
                    }
                }



                onCurrentIndexChanged: {

                    if(currentIndex!=-1)
                    {
                        var sqlid=model[currentIndex].sqlid;
                        combotr.getsqlid(sqlid,1,Query.SelectSubParam,Query.Subq,"TRC",1);
                        TaskResult.taskresult.HatBilgisi_TR=sqlid ;
                        trsCombo.enabled=true;


                    }

                    else
                        trsCombo.enabled=false;

                    trsCombo.currentIndex=-1;
                }


        }

1 个答案:

答案 0 :(得分:0)

我遇到了一个非常类似的问题,但我有一个ListView和一个文本框,它应该适用于你的问题。

首先,没有(简单)在QML中过滤模型的可能性,但我遇到了解决此问题的项目:(QML)SortFilterProxyModel

我担心目前无法尝试,但您可以按照以下方式使用此模型:

import SortFilterProxyModel 0.1

ComboBox {
    textRole: "value"
    model: SortFilterProxyModel {
        id: filteredModel
        sourceModel: selectionDialog.model
        filterRoleName: parent.textRole
        filterPattern: parent.text // not pretty shure if this is correct,
        // sorry I currently cannot test it, use the inserted Text of the Combobox
        filterCaseSensitivity: Qt.CaseInsensitive
    }
}

我希望这会对你有所帮助。随意询问是否有不清楚的事情。