littleedit.qml
正常情况下,QT的文本控件输入功能无需QML参与,但在一些无键盘的特殊场景,只有触屏可用时,可以使用一些虚拟键盘,包括操作系统自带的tabtip.exe等。另外,还可以使用QML自带的虚拟键盘,对于非纯QML应用,则需要进行一些适当的搭配转换。
main.cpp
#include <QApplication>
#include <QLineEdit>
#include <QQmlApplicationEngine>
#include <QQuickWidget>
#include <QLineEdit>
int main(int argc, char *argv[])
{
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QWidget normWidget;
normWidget.setFixedSize(1000,1000);
normWidget.show();
QLineEdit* pEdit = new QLineEdit(&normWidget);
pEdit->setText("qwidget rcv qml");
pEdit->setFixedSize(800,80);
pEdit->show();
QQuickWidget * qmlInputPanel = new QQuickWidget(&normWidget);
qmlInputPanel->setResizeMode(QQuickWidget::SizeViewToRootObject);
qmlInputPanel->setSource(QStringLiteral("qrc:/littleedit.qml"));
qmlInputPanel->move(60,120);
qmlInputPanel->setFocusProxy(pEdit);
qmlInputPanel->show();
return app.exec();
}
littleedit.qml
import QtQuick 2.0
import QtQuick.Window 2.0
import QtQuick.VirtualKeyboard 2.0
InputPanel {
id: inputPanel
z: 99
x: 0
y: 0
width: 400
height:120
}
pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += quickwidgets virtualkeyboard
CONFIG += c++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
qml.qrc
效果如下:
该博客介绍了如何在QT应用程序中,特别是在无键盘设备上,利用QML的虚拟键盘功能。通过设置环境变量和QML组件,实现了QLineEdit与QQuickWidget的结合,展示了一个在QQuickWidget中加载QML文件的例子,从而在QLineEdit输入框中启用QML虚拟键盘。
8047

被折叠的 条评论
为什么被折叠?



