QQuickWidget 中的 Qt 虚拟键盘

Qt Virtual Keyboard in QQuickWidget

是否可以在 QQuickWidget 或 QWidget 中显示虚拟键盘?

我有 QWidget 应用程序,我需要更好地控制显示 VirtualKeyboard 的位置。

今天我花了一整天的时间寻找解决方案,不幸的是没有成功。

以下代码说明在QQuickWidget中使用虚拟键盘是有效的

main.cpp

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQuickWidget>

int main(int argc, char *argv[])
{
    qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);
    QQuickWidget widget;
    widget.setResizeMode(QQuickWidget::SizeRootObjectToView);
    widget.setSource(QStringLiteral("qrc:/main.qml"));
    widget.show();
    return app.exec();
}

main.qml

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.VirtualKeyboard 2.14

Rectangle {
    id: window
    width: 640
    height: 480

    TextEdit{
        text: "Hello world"
        anchors.centerIn: parent
    }

    InputPanel {
        id: inputPanel
        z: 99
        x: 0
        y: window.height
        width: window.width

        states: State {
            name: "visible"
            when: inputPanel.active
            PropertyChanges {
                target: inputPanel
                y: window.height - inputPanel.height
            }
        }
        transitions: Transition {
            from: ""
            to: "visible"
            reversible: true
            ParallelAnimation {
                NumberAnimation {
                    properties: "y"
                    duration: 250
                    easing.type: Easing.InOutQuad
                }
            }
        }
    }
}

qml.qrc

<RCC>
    <qresource prefix="/">
        <file>main.qml</file>
    </qresource>
</RCC>

59777221.pro

QT += quickwidgets virtualkeyboard
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES +=  main.cpp
RESOURCES += qml.qrc
├── 59777221.pro
├── main.cpp
├── main.qml
└── qml.qrc