2

我在 QML 中有一个包含 2000 多个项目的 ComboBox。滚动到最后一个元素的项目太多。如何为 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;
                }


        }
4

1 回答 1

0

我遇到了一个非常相似的问题,但是我有一个 ListView 和一个 Textbox 它应该适用于您的问题。

首先,在 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
    }
}

我希望这能帮到您。随意询问是否有不清楚的地方。

于 2016-10-26T12:22:48.360 回答