这是一个使用Qt虚拟键盘的QML文本输入示例。

该示例提供两种使用方式:一种用于桌面平台,另一种用于嵌入式平台,本例采用嵌入式平台方式显示。它们区别是前者脱离窗口应用于全局,后者依附于窗口。
disable-desktop|android-embedded|!isEmpty(CROSS_COMPILE)|qnx {
DEFINES += MAIN_QML=\\\"basic-b2qt.qml\\\" # 采用了自定义的虚拟键盘的InputPanel,可以依附窗口。
} else {
DEFINES += MAIN_QML=\\\"Basic.qml\\\" # 独立于窗口
}
0x00 使用(Qt5.7版本以上)
- 代码添加
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
- Qml文件导入模块
import QtQuick.VirtualKeyboard 2.1
- TextInput派生类类带有TextInput派生类的控件获得输入焦点即可调用出虚拟键盘。
0x01 根据不同的输入方式显示不同的键盘
- 设置
inputMethodHints
变量即可
TextField {
id: digitsField
width: parent.width
placeholderText: "Digits only field" /* 输入为空时显示的提示文字 */
inputMethodHints: Qt.ImhDigitsOnly /* 输入策略 */
enterKeyAction: EnterKeyAction.Next /* 键盘确定键策略 */
onAccepted: textArea.focus = true /* 结束输入操作行为 */
}
- 常用值有:
inputMethodHints | 描述 |
---|---|
ImhNone | 默认键盘(默认值) |
ImhDigitsOnly | 数字键盘 |
ImhUppercaseOnly | 大写键盘 |
ImhLowercaseOnly | 小写键盘 |
ImhFormattedNumbersOnly | 数字键盘 + 逻辑符号键盘 |
0x02 预览
- 默认键盘

- 数字键盘

- 数字键盘 + 逻辑符号键盘

- 支持切换输入法

0x03 关于更多
- 在QtCreator软件可以找到:

- 或在以下Qt安装目录找到:
C:\Qt\{你的Qt版本}\Examples\{你的Qt版本}\virtualkeyboard\basic
- 相关链接
https://doc.qt.io/qt-5/qtvirtualkeyboard-basic-example.html
- Qt君公众号回复『Qt示例』获取更多内容。
网友评论