Qt QML使用虛擬鍵盤(pán)的示例代碼
示例效果

使用"虛擬鍵盤(pán)"注意 (例子的Qt版本:5.12.4)
注意一:
/* 必須在main.cpp開(kāi)始處加入如下代碼,否則無(wú)法使用"虛擬鍵盤(pán)" */
qputenv(“QT_IM_MODULE”,QByteArray(“qtvirtualkeyboard”));
注意二:
鍵盤(pán)大小是根據(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);
// 必須加入否則無(wú)法使用"虛擬鍵盤(pán)"
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("用戶(hù)名:")
font.family: "微軟雅黑"
font.pixelSize: 20
}
TextField
{
placeholderText: "輸入用戶(hù)名.."
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"] // 英語(yǔ)、中文、日語(yǔ) (若不設(shè)置,則語(yǔ)言就有很多種)
}
}
}
以上就是Qt QML使用虛擬鍵盤(pán)的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Qt QML虛擬鍵盤(pán)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)的統(tǒng)計(jì)素?cái)?shù)并求和代碼分享
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)的統(tǒng)計(jì)素?cái)?shù)并求和代碼分享,來(lái)自PAT平臺(tái)(浙江大學(xué)計(jì)算機(jī)程序設(shè)計(jì)能力考試系統(tǒng))的一個(gè)題目,需要的朋友可以參考下2014-08-08
C++實(shí)現(xiàn)LeetCode(104.二叉樹(shù)的最大深度)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(104.二叉樹(shù)的最大深度),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++類(lèi)重載函數(shù)的function和bind使用示例
這篇文章主要介紹了C++類(lèi)重載函數(shù)的function和bind使用示例,幫助大家更好的理解和使用c++,感興趣的朋友可以了解下2021-01-01
C語(yǔ)言實(shí)現(xiàn)為無(wú)聲avi視頻添加wave音樂(lè)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言如何實(shí)現(xiàn)為無(wú)聲avi視頻添加wave音樂(lè),文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以了解一下2023-11-11
大數(shù)據(jù)情況下桶排序算法的運(yùn)用與C++代碼實(shí)現(xiàn)示例
在排序元素很多的情況下,其實(shí)桶排序的性能并不是太高,這里我們配合單鏈表的直接插入排序,來(lái)看下一大數(shù)據(jù)情況下桶排序算法的運(yùn)用與C++代碼實(shí)現(xiàn)示例:2016-07-07
詳解C++設(shè)計(jì)模式編程中對(duì)狀態(tài)模式的運(yùn)用
這篇文章主要介紹了C++設(shè)計(jì)模式編程中對(duì)狀態(tài)模式的運(yùn)用,狀態(tài)模式允許一個(gè)對(duì)象在其內(nèi)部狀態(tài)改變時(shí)改變它的行為,對(duì)象看起來(lái)似乎修改了它的類(lèi),需要的朋友可以參考下2016-03-03
如何實(shí)現(xiàn)一定概率選中某一個(gè)字母
本篇文章是對(duì)如何實(shí)現(xiàn)一定概率選中某一個(gè)字母的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

