Qt QML使用虛擬鍵盤的示例代碼
示例效果

使用"虛擬鍵盤"注意 (例子的Qt版本:5.12.4)
注意一:
/* 必須在main.cpp開始處加入如下代碼,否則無法使用"虛擬鍵盤" */
qputenv(“QT_IM_MODULE”,QByteArray(“qtvirtualkeyboard”));
注意二:
鍵盤大小是根據(jù)寬度自動(dòng)計(jì)算的,所以,應(yīng)用程序應(yīng)該只設(shè)置InputPanel 的寬度和y 坐標(biāo),不能設(shè)置高度。
源碼
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
// 必須加入否則無法使用"虛擬鍵盤"
qputenv("QT_IM_MODULE",QByteArray("qtvirtualkeyboard"));
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.5
import QtQuick.VirtualKeyboard 2.2
import QtQuick.VirtualKeyboard.Settings 2.2
Window
{
id: root
visible: true
width: 800
height: 600
title: qsTr("Hello World")
ColumnLayout
{
anchors.top: parent.top
anchors.topMargin: root.height * 0.2
anchors.horizontalCenter: parent.horizontalCenter
spacing: 25
RowLayout
{
spacing: 25
Text
{
text: qsTr("用戶名:")
font.family: "微軟雅黑"
font.pixelSize: 20
}
TextField
{
placeholderText: "輸入用戶名.."
font.family: "微軟雅黑"
font.pixelSize: 16
Layout.preferredWidth: root.width * 0.25
background: Rectangle
{
radius: 4
border.color: parent.focus ? "#498ff8" : "#C4DBFC"
}
}
}
RowLayout
{
spacing: 25
Text
{
text: qsTr("密 碼:")
font.family: "微軟雅黑"
font.pixelSize: 20
}
TextField
{
placeholderText: "輸入密碼.."
font.family: "微軟雅黑"
font.pixelSize: 16
Layout.preferredWidth: root.width * 0.25
background: Rectangle
{
radius: 4
border.color: parent.focus ? "#498ff8" : "#C4DBFC"
}
}
}
}
InputPanel
{
id: inputPannelID
z: 99
y: root.height // 默認(rèn)讓其處于窗口最下方,貌似隱藏一樣
width: root.width
visible: true // 一直顯示
states: State
{
name: "visible"
when: inputPannelID.active
PropertyChanges
{
target: inputPannelID
y: root.height-inputPannelID.height
}
}
transitions: Transition
{
from: ""
to: "visible"
reversible: true
ParallelAnimation
{
NumberAnimation
{
properties: "y"
duration: 250
easing.type: Easing.InOutQuad
}
}
}
Component.onCompleted:
{
VirtualKeyboardSettings.styleName = "retro" // 復(fù)古樣式
VirtualKeyboardSettings.wordCandidateList.alwaysVisible = true
VirtualKeyboardSettings.activeLocales = ["en_US","zh_CN","ja_JP"] // 英語、中文、日語 (若不設(shè)置,則語言就有很多種)
}
}
}
以上就是Qt QML使用虛擬鍵盤的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Qt QML虛擬鍵盤的資料請關(guān)注腳本之家其它相關(guān)文章!
- Qt6 QML實(shí)現(xiàn)DateTimePicker組件的示例代碼
- Qt?QML實(shí)現(xiàn)無邊框窗口的實(shí)例代碼
- QML與C++交互之創(chuàng)建自定義對象的實(shí)現(xiàn)
- Qt6+QML實(shí)現(xiàn)Windows屏幕錄制功能
- Qt?Qml實(shí)現(xiàn)毛玻璃效果
- Qt qml實(shí)現(xiàn)動(dòng)態(tài)輪播圖效果
- 基于Qml實(shí)現(xiàn)水印工具
- QML與C++幾種交互方式
- C++與QML進(jìn)行數(shù)據(jù)交互的常見方法總結(jié)
- C++與QML交互的項(xiàng)目實(shí)踐
- Qt6 QML Flickable控件詳解
相關(guān)文章
C語言函數(shù)調(diào)用底層實(shí)現(xiàn)原理分析
這篇文章主要介紹了C語言函數(shù)調(diào)用底層實(shí)現(xiàn)原理,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
深入分析C語言分解質(zhì)因數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了深入分析C語言分解質(zhì)因數(shù)的實(shí)現(xiàn)方法,作者結(jié)合了ACM題目作為相關(guān)拓展,需要的朋友可以參考下2015-08-08
C語言數(shù)據(jù)結(jié)構(gòu)中樹與森林專項(xiàng)詳解
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)中樹與森林,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11
C++使用expected實(shí)現(xiàn)優(yōu)雅的錯(cuò)誤處理
C++ 中提供了很多中方式進(jìn)行錯(cuò)誤處理。無論是通過拋異常還是通過錯(cuò)誤碼,標(biāo)準(zhǔn)庫都提供相應(yīng)的調(diào)用,今天本文為大家介紹的是使用expected進(jìn)行錯(cuò)誤處理,感興趣的可以了解一下2023-06-06

