QML textInput元素中的自动完成和suggesstion

2 8*_*2 8 7 c++ qt autocomplete pyside qml

我有一个像这样的QML textInput元素:

TextBox.qml

FocusScope {
    id: focusScope
    property int fontSize: focusScope.height -30
    property int textBoxWidth: parent.width * 0.8
    property int textBoxHeight: 45
    property string placeHolder: 'Type something...'
    property bool isUserInTheMiddleOfEntringText: false
    width: textBoxWidth
    height: textBoxHeight

    Rectangle {
        width: parent.width
        height: parent.height
        border.color:'blue'
        border.width: 3
        radius: 0
        MouseArea {
            anchors.fill: parent
            onClicked: {
                focusScope.focus = true
                textInput.openSoftwareInputPanel()
            }
        }
    }
    Text {
        id: typeSomething
        anchors.fill: parent; anchors.rightMargin: 8
        verticalAlignment: Text.AlignVCenter
        text: placeHolder
        color: 'red'
        font.italic: true
        font.pointSize: fontSize
        MouseArea {
            anchors.fill: parent
            onClicked: {
                focusScope.focus = true
                textInput.openSoftwareInputPanel()
            }
        }

    }

    MouseArea {
        anchors.fill: parent
        onClicked: {
            focusScope.focus = true
            textInput.openSoftwareInputPanel()
        }
    }

    TextInput {
        id: textInput
        anchors {
            right: parent.right
            rightMargin: 8
            left: clear.right
            leftMargin: 8
            verticalCenter: parent.verticalCenter
        }
        focus: true
        selectByMouse: true
        font.pointSize: fontSize
    }



    Text {
        id: clear
        text: '\u2717'
        color: 'yellow'
        font.pointSize: 25
        opacity: 0
        visible: readOnlyTextBox ? false : true
        anchors {
            left: parent.left
            leftMargin: 8
            verticalCenter: parent.verticalCenter
        }
        MouseArea {
            anchors.fill: parent
            onClicked: {
                textInput.text = ''
                focusScope.focus = true;
                textInput.openSoftwareInputPanel()
            }
        }
    }

    states: State {
        name: 'hasText'; when: textInput.text != ''
        PropertyChanges {
            target: typeSomething
            opacity: 0
        }
        PropertyChanges {
            target: clear
            opacity: 0.5
        }
    }

    transitions: [
        Transition {
            from: ''; to: 'hasText'
            NumberAnimation {
                exclude: typeSomething
                properties: 'opacity'
            }
        },
        Transition {
            from: 'hasText'; to: ''
            NumberAnimation {
                properties: 'opacity'
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想在此文本框中添加自动填充功能和谷歌搜索等建议.自动填充从数据库和数据库获取数据通过pyside SLOT返回一个字典列表.(或c ++插槽)

我怎么做这项工作?

dan*_*nt3 16

看看这段代码:https://github.com/jturcotte/liquid/blob/master/qml/content/SuggestionBox.qml

我打赌它会完成这项工作.

编辑:

上面链接的代码有点复杂,需要C++后端,所以我简化了它并制作了纯Qml示例应用程序,您可以使用,编辑一点并应用于您的需求.来源可以在这里找到.最重要的是:

  1. 这个SuggestionBox的实现使用某种模型作为完成/建议某事的源
  2. 每次用户点击项目时,都会发出信号itemSelected(item)
  3. 应用程序的主要组件,将其LineEdit组件绑定到SuggestionBox

请注意,代码非常粗糙并且为了示例而编写.

  • 谢谢.你能告诉我怎么用这段代码? (2认同)