根据QObject调整qtvirtualkeyboard的大小

aid*_*a_m 1 c++ qt qtvirtualkeyboard

我正在使用 qml qtvirtual 键盘: https: //github.com/qt/qtvirtualkeyboard

我正在尝试将它与基于小部件的 Qt 应用程序的其余部分“连接”。例如,当我单击 QLineEdit 时,我希望键盘显示出来并在应用程序上下文中像物理键盘一样工作。

为此,我安装了 qtvirtualkeyboard/src 中的内容(qmake && make && make install),这是我的 main.cpp :

#include <QQuickView>
#include <QApplication>
#include <QQmlEngine>
#include <QQmlContext>

int main(int argc, char *argv[]){

    qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));

    QApplication app(argc, argv);
    MainWindow w();  // This is a QWidget / QMainWindow
    w.setFixedSize(800, 480);
    w.show();

    return app.exec();
}
Run Code Online (Sandbox Code Playgroud)

当我在桌面上执行此操作时,键盘占据了我屏幕的一半,尽管我的应用程序是 800x480。

当我在带有 7 英寸触摸屏的 Raspberry Pi 上执行它时,键盘占据了页面的一半,并且顶部出现黑色边框。

我想自己修复键盘尺寸。当我创建带有项目等的 QML 文件时,键盘不再显示。

Mat*_*ger 6

在过去的一个月左右的时间里,我一直在与 QtVirtualKeyboard 怪物搏斗。希望您能从我的尝试和错误中受益。

QtVirtualKeyboard 拼图的各个部分

要知道/记住的是,本质上有四个独立的部分来通知键盘的整体功能和实现:

  1. qt虚拟键盘(函数)
  2. 布局(结构)
  3. 风格(演示)
  4. 应用程序中的 Qml(实施)

如果您使用的是 qtvirtualkeyboard 附带的默认布局和样式(换句话说,您还没有创建任何自定义样式或布局),那么值得看一下它们,以便更好地了解其背后发生的情况。场景。您可以在这里找到它们:

布局: /path/to/qtvirtualkeyboard/src/virtualkeyboard/content/layouts

款式: /path/to/qtvirtualkeyboard/src/virtualkeyboard/content/styles

放置键盘

这是一个(有点过于简化的)示例 Qml 文件,演示了如何定位键盘。就您而言,由于您关心键盘消耗了多少垂直屏幕空间,因此键盘的 y 属性是您首先要瞄准的目标。

首先,将keyboard.y 设置为屏幕的高度(例如parent.height 或您想要获取该信息的任何高度)。将键盘顶部放在屏幕底部可以将其隐藏起来。

然后,当您单击 TextInput 字段并调用 QtVirtualKeyboard 时,在状态从默认/初始更改为“可见”期间,将 Keyboard.y 属性更改为 TextInput 字段的底部。通过状态更改处理此问题的另一个好处是您可以控制键盘动画。

import QtQuick 2.7
import QtQuick.VirtualKeyboard 2.1

Item {
    id: appSectionWithTextInput

    property int screenHeight: parent.height; // the height of the screen/display
    anchors.fill: parent;

    TextInput {
        id: textInput;
        height: 120;
        width: parent.width - 2;

        color: "#000000"; // black

        // http://doc.qt.io/qt-5/qinputmethod.html#properties
        focus: Qt.inputMethod.visible;

        verticalAlignment: TextInput.AlignVCenter;
    }

    InputPanel {
        id: keyboard;
        y: screenHeight; // position the top of the keyboard to the bottom of the screen/display

        anchors.left: parent.left;
        anchors.right: parent.right;

        states: State {
            name: "visible";
            when: keyboard.active;
            PropertyChanges {
                target: keyboard;
                // position the top of the keyboard to the bottom of the text input field
                y: textInput.height;
            }
        }
        transitions: Transition {
            from: ""; // default initial state
            to: "visible";
            reversible: true; // toggle visibility with reversible: true;
            ParallelAnimation {
                NumberAnimation {
                    properties: "y";
                    duration: 250;
                    easing.type: Easing.InOutQuad;
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

自定义布局

如果您想创建自己的自定义布局,最好的选择是将 qtvirtualkeyboard 附带的现有布局之一(例如 en_GB)复制到您选择的目录中。到达那里后,将其重命名为您喜欢的名称并添加以下环境变量:QT_VIRTUALKEYBOARD_LAYOUT_PATH=/path/to/custom/keyboard-layout/mycustomlayout。

注意:在运行应用程序之前,应将 QT_VIRTUALKEYBOARD_LAYOUT_PATH 环境变量设置为包含自定义键盘布局的文件系统目录。

定制风格

如果您想创建自己的自定义键盘样式,请参阅此页面以了解应在其中创建自定义样式目录的特定目录。

到达那里后,添加以下环境变量:QT_VIRTUALKEYBOARD_STYLE=mycustomstyle。

如果您想直接操作键盘的大小,您可以访问/更改位于自定义样式目录中的 style.qml 文件中的这些属性。

// Properties
keyboardDesignWidth
keyboardDesignHeight
keyboardRelativetopMargin
keyboardRelativerightMargin
keyboardRelativeBottomMargin
keyboardRelativeLeftMargin
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到这些属性的完整列表。

祝你好运!