【问题标题】:Autocomplete and suggesstion in QML textInput elementQML textInput 元素中的自动完成和建议
【发布时间】:2013-03-29 17:43:03
【问题描述】:

我有一个像这样的 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'
            }
        }
    ]
}

我想在此文本框中添加自动完成和建议,如谷歌搜索。自动完成从数据库中获取数据,数据库通过 pyside SLOT(或 c++ 插槽)返回字典列表

我怎样才能完成这项工作?

【问题讨论】:

    标签: c++ qt autocomplete qml pyside


    【解决方案1】:

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

    我敢打赌它会完成这项工作。

    编辑:

    上面链接的代码有些复杂,需要 C++ 后端,所以我对其进行了简化并制作了纯 Qml 示例应用程序,您可以使用它,稍微编辑一下并应用到您的需要。来源可以找到here。最重要的是:

    1. This implementation of SuggestionBox 使用某种模型作为完成/建议某事的来源
    2. 它的信号 itemSelected(item) 将在用户每次点击项目时发出
    3. Main component of applicationbinds its LineEdit component to SuggestionBox

    请注意,代码非常粗糙,只是为了示例而编写的。

    【讨论】:

    • 谢谢。这个建议很好,但建议不能用鼠标和键盘方向键选择。你能把它添加到你的代码中吗?
    • 请看我的评论@dant3
    • @varahram 见this edit
    【解决方案2】:

    我一直在寻找非常相似的东西:围绕QML TextField 构建的 QML 自动完成组件,而不是问题中的较低级别、更灵活但工作量更大的 TextInput。

    因为我找不到,所以我实现了它。如果有人想使用它:它已获得 MIT 许可,可作为 an application 的一部分提供,我正在开发。您在src/qml/AutoComplete.qml 中找到该组件,该应用程序可以作为使用示例。特点:

    • 以粗体突出显示自动完成的字符,就像在 Google 搜索中一样
    • 键绑定(使用箭头键导航、Return / Enter、Esc 关闭完成框、Esc Esc 取消焦点)
    • 目前使用简单的QStringList 作为模型,应用程序展示了如何在按下下一个键时使用实时 SQL 数据库查询更新模型
    • 大量文档化的代码,因此应该很容易适应

    让我知道这是否有用,然后我可能会将其打包为 Qt QPM package,甚至尝试使其足够成熟以添加到 QML UI 库 KDE Kirigami

    【讨论】:

    • 嘿,你最终把它作为一个包发布了吗?
    • @Curtwagner1984 不,因为您是第一个对此表示兴趣的人。但它只是一个 QML 文件,所以现在应该复制粘贴。
    • 问题在于它依赖于Kirigami
    猜你喜欢
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 2020-07-14
    相关资源
    最近更新 更多