欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > QML使用Qt自带软键盘例子

QML使用Qt自带软键盘例子

2024/10/26 1:18:26 来源:https://blog.csdn.net/Hat_man_/article/details/142601086  浏览:    关键词:QML使用Qt自带软键盘例子

//注意:一定要保证Qt有安装VirtualKeyboard插件

import QtQuick 2.10
import QtQuick.Window 2.3
import QtQuick.Controls 2.3
import QtQuick.VirtualKeyboard 2.1
import QtQuick.VirtualKeyboard.Settings 2.1

Window {
    id: root
    visible: true
    width: 1000
    height: 600
    color: "#F6F6F6"

    // 使窗口强制获得焦点
    MouseArea {
        anchors.fill: parent
        onClicked: forceActiveFocus()
    }

    TextField {
        id: textUser
        placeholderText: "请输入用户名"
        font.family: "Microsoft YaHei"
        font.pixelSize: 28
        topPadding: 10
        anchors.top: parent.top
        anchors.topMargin: 40
        anchors.left: parent.left
        anchors.leftMargin: 40

        background: Rectangle {
            implicitWidth: 424
            implicitHeight: 50
            radius: 4
            border.color: parent.focus  ? "#498ff8" : "#C4DBFC"
        }

        // 当选择输入框的时候才显示键盘
        onPressed: {
            inputX = x
            inputY = y + height
            inputPanel.visible = true
        }
    }

    TextField {
        id: textPasswd
        placeholderText: "请输入密码"
        font.family: "Microsoft YaHei"
        font.pixelSize: 28
        topPadding: 10
        anchors.top: parent.top
        anchors.topMargin: 120
        anchors.left: parent.left
        anchors.leftMargin: 40

        background: Rectangle {
            implicitWidth: 424
            implicitHeight: 50
            radius: 4
            border.color: parent.focus  ? "#498ff8" : "#C4DBFC"
        }

        // 当选择输入框的时候才显示键盘
        onPressed: {
            inputX = x
            inputY = y + height
            inputPanel.visible = true
        }
    }

    property int inputX: 0 // 键盘x坐标(动态)
    property int inputY: root.height // 键盘y坐标(动态)

    // 嵌入式虚拟键盘
    InputPanel {
        id: inputPanel
        z: 99
        //更改x,y即可更改键盘位置
        x: textUser.x
        y: root.height
        //更改width即可更改键盘大小
        width: root.width - 100
        visible: false

        externalLanguageSwitchEnabled: false

        states: State {
            name: "visible"
            when: inputPanel.active
            PropertyChanges {
                target: inputPanel
                // 将键盘顶部放在屏幕底部会使其隐藏起来
                x: inputX
                y: inputY
            }
        }

        Component.onCompleted: {
            VirtualKeyboardSettings.locale = "eesti" // 复古键盘样式
            VirtualKeyboardSettings.wordCandidateList.alwaysVisible = true

            // 允许的语言,重要!
            VirtualKeyboardSettings.activeLocales = ["en_GB","zh_CN"]
            // 默认的语言,重要!
            VirtualKeyboardSettings.locale = "en_GB"
        }

        // 这种集成方式下点击隐藏键盘的按钮是没有效果的,只会改变active,因此我们自己处理一下
        onActiveChanged: {
            if(!active) { visible = false }
        }
    }

    Button{
        id:btn_Language
        text: "切换语言"
        width:200
        height: 50
        topPadding: 10
        anchors.top: parent.top
        anchors.topMargin: 200
        anchors.left: parent.left
        anchors.leftMargin: 100
        onClicked: {
            if (VirtualKeyboardSettings.locale === "en_GB") {
                VirtualKeyboardSettings.locale = "zh_CN";
                text = "切换语言(中文)"
            } else {
                VirtualKeyboardSettings.locale = "en_GB";
                text = "切换语言(英文)"
            }
        }
    }
}
 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com