QLineEdit 中使用QML原生虚拟键盘Qt5VirtualKeyboard

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

效果如下:

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值