仮想キーボードQt Quick

この例では、Qt Quick アプリケーションで仮想キーボードを使用する方法を示します。

この例には2つの実装があります:1つはデスクトッ ププラットフォーム用、もう1つは組み込みプラットフォーム用です。前者のバージョンは、仮想キーボードを使用して複数のテキスト・フィールドにテキスト入力を可能にし、後者のバージョンは、同じUIを使用しますが、カスタム仮想キーボードInputPanel 。以下のスニペットは、CONFIGオプションに基づいて適切な実装を選択するようにプロジェクトを設定する方法を示しています:

  • qmake (basic.pro):
    !qtConfig(vkb-desktop) {
        DEFINES += MAIN_QML=\\\"basic-b2qt.qml\\\"
    } else {
        DEFINES += MAIN_QML=\\\"Basic.qml\\\"
    }
  • CMake (CMakeLists.txt):
    if(NOT QT_FEATURE_vkb_desktop)
        target_compile_definitions(basic PUBLIC
            MAIN_QML="basic-b2qt.qml"
        )
    endif()
        ...
    if(QT_FEATURE_vkb_desktop)
        target_compile_definitions(basic PUBLIC
            MAIN_QML="Basic.qml"
        )
    endif()

この例では、.qml ファイルをロードする前にQT_IM_MODULE 環境変数を設定することで、仮想キーボードを有効にしています:

#include <QQuickView>
#include <QGuiApplication>
#include <QQmlEngine>

int main(int argc, char *argv[])
{
    qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));

    QGuiApplication app(argc, argv);
    QQuickView view(QString("qrc:/%2").arg(MAIN_QML));
    if (view.status() == QQuickView::Error)
        return -1;
    view.setResizeMode(QQuickView::SizeRootObjectToView);

    view.show();

    return app.exec();
}

これに加えて、TextFieldTextArea のカスタム・アイテムを使用して、EnterKeyAction のアタッチ・プロパティを使用して[ENTER]キーの動作を設定します。

import QtQuick
import QtQuick.Controls
import QtQuick.VirtualKeyboard
import "content"

Rectangle {
    ...
                TextField {
                    width: parent.width
                    placeholderText: "One line field"
                    enterKeyAction: EnterKeyAction.Next
                    onAccepted: passwordField.focus = true
                }
    ...
                TextArea {
                    id: textArea
                    width: parent.width
                    placeholderText: "Multiple line field"
                    height: Math.max(206, implicitHeight)
                }
}

TextFieldTextArea コントロールは、enterKeyEnabledenterKeyAction プロパティで、それぞれのQt Quick コントロール 2タイプを拡張します。スニペット内のTextFieldTextArea インスタンスは、これらのプロパティを設定して、デフォルトの動作を変更することができます。

例の実行

からサンプルを実行するには Qt Creatorからサンプルを実行するには、Welcome モードを開き、Examples からサンプルを選択します。詳細については、Qt Creator:Tutorialを参照してください:ビルドと実行

サンプル・プロジェクト @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.