c++ - QML textInput 元素中的自动完成和建议

标签 c++ qt autocomplete qml pyside

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

文本框.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++ 插槽)返回字典列表

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

最佳答案

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

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

编辑:

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

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

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

关于c++ - QML textInput 元素中的自动完成和建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15865603/

相关文章:

jquery - JQuery 1.4 的 Visual Studio 自动完成功能在哪里?

c++ - QT Creator 无法识别新项目的 STL 类型

c++ - 为什么 "unreferenced formal parameter"让我担心?

c++ - 使用 2 个队列实现堆栈

由于 QSslSocket 的 setLocalCertificate,Qt 控制台应用程序崩溃

c++ - QWidget::setLayout: 试图在 Widget ""上设置 QLayout "",它已经有一个布局

vim - 使 AutoComplPop 允许连字符作为单词字符

c++ - 打印最大元素的索引

c++ - 如何在 c++/QT 中以编程方式右键单击文件/文件夹时添加自定义菜单?

jquery - Solr JQuery 自动完成不会自动完成