Qt QML使用虛擬鍵盤的示例代碼
示例效果
使用"虛擬鍵盤"注意 (例子的Qt版本:5.12.4)
注意一:
/* 必須在main.cpp開始處加入如下代碼,否則無法使用"虛擬鍵盤" */
qputenv(“QT_IM_MODULE”,QByteArray(“qtvirtualkeyboard”));
注意二:
鍵盤大小是根據(jù)寬度自動計算的,所以,應用程序應該只設置InputPanel 的寬度和y 坐標,不能設置高度。
源碼
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 // 默認讓其處于窗口最下方,貌似隱藏一樣 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" // 復古樣式 VirtualKeyboardSettings.wordCandidateList.alwaysVisible = true VirtualKeyboardSettings.activeLocales = ["en_US","zh_CN","ja_JP"] // 英語、中文、日語 (若不設置,則語言就有很多種) } } }
以上就是Qt QML使用虛擬鍵盤的示例代碼的詳細內(nèi)容,更多關(guān)于Qt QML虛擬鍵盤的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語言實現(xiàn)的統(tǒng)計素數(shù)并求和代碼分享
這篇文章主要介紹了C語言實現(xiàn)的統(tǒng)計素數(shù)并求和代碼分享,來自PAT平臺(浙江大學計算機程序設計能力考試系統(tǒng))的一個題目,需要的朋友可以參考下2014-08-08C++實現(xiàn)LeetCode(104.二叉樹的最大深度)
這篇文章主要介紹了C++實現(xiàn)LeetCode(104.二叉樹的最大深度),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07C++類重載函數(shù)的function和bind使用示例
這篇文章主要介紹了C++類重載函數(shù)的function和bind使用示例,幫助大家更好的理解和使用c++,感興趣的朋友可以了解下2021-01-01大數(shù)據(jù)情況下桶排序算法的運用與C++代碼實現(xiàn)示例
在排序元素很多的情況下,其實桶排序的性能并不是太高,這里我們配合單鏈表的直接插入排序,來看下一大數(shù)據(jù)情況下桶排序算法的運用與C++代碼實現(xiàn)示例:2016-07-07