qt - QML 虚拟键盘隐藏按钮不起作用

标签 qt keyboard qml qtvirtualkeyboard

如果我点击键盘隐藏按钮,我会遇到问题。以下是代码:

import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtQuick.VirtualKeyboard 2.2

Window {
    visible: true
    width: 600
    height: 500
    title: qsTr("Hello World")

    TextField {
        id: textfield
        anchors.bottom:(inputPanel.visible) ? inputPanel.top : parent.bottom
        color: "#2B2C2E"
        cursorVisible: activeFocus
        selectionColor: Qt.rgba(0.0, 0.0, 0.0, 0.15)
        selectedTextColor: color
    }

    InputPanel {
        id: inputPanel
        z: 89
        anchors.bottom:parent.bottom
        anchors.left: parent.left
        anchors.right: parent.right

        visible: Qt.inputMethod.visible //** Warning here 

    }
}

以下是用例:

  1. 如果我点击TextField,键盘会按预期弹出,但当我点击隐藏键盘按钮时,它不会隐藏

  2. 如果我点击TextField键盘按预期弹出,接下来如果我双击TextField,然后点击隐藏键盘按钮,它就会隐藏

我还收到警告:

QML InputPanel: Binding loop detected for property "visible"

请提出建议。

最佳答案

basic example当其 active 属性为 true 时显示输入面板:

InputPanel {
    id: inputPanel
    z: 89
    y: appContainer.height
    anchors.left: parent.left
    anchors.right: parent.right
    states: State {
        name: "visible"
        /*  The visibility of the InputPanel can be bound to the Qt.inputMethod.visible property,
            but then the handwriting input panel and the keyboard input panel can be visible
            at the same time. Here the visibility is bound to InputPanel.active property instead,
            which allows the handwriting panel to control the visibility when necessary.
        */
        when: inputPanel.active
        PropertyChanges {
            target: inputPanel
            y: appContainer.height - inputPanel.height
        }
    }
    transitions: Transition {
        id: inputPanelTransition
        from: ""
        to: "visible"
        reversible: true
        enabled: !VirtualKeyboardSettings.fullScreenMode
        ParallelAnimation {
            NumberAnimation {
                properties: "y"
                duration: 250
                easing.type: Easing.InOutQuad
            }
        }
    }
    Binding {
        target: InputContext
        property: "animating"
        value: inputPanelTransition.running
    }
}

所以你可以做类似的事情:

InputPanel {
    id: inputPanel
    z: 89
    anchors.bottom:parent.bottom
    anchors.left: parent.left
    anchors.right: parent.right

    visible: active
}

关于qt - QML 虚拟键盘隐藏按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45784405/

相关文章:

CSS如何防止键盘将内容向上移动?

qt - QML:有条件地设置属性组的不同属性

c++ - 即时翻译 Qt QML 应用程序

c++ - FOO::BAR 类命名空间的 Qt MOC 错误

c++ - 如何绘制变暗的QPixmap?

c++ - qnetwork没有回复状态码或错误,但失败

python - QML Python 中的自定义对象引用

qt - 在 Ubuntu 上编译 QScintilla 失败

c# - 将键盘宏命令发送到游戏窗口

javascript - 如何向现有的 JavaScript 函数添加键盘快捷键?