Nesta página

Teclado virtual em Qt Quick

Este exemplo mostra como usar o teclado virtual em um aplicativo Qt Quick.

Janela do aplicativo com campos de texto rotulados acima do teclado virtual para digitação de texto

O exemplo possui duas implementações: uma para plataformas de desktop e outra para plataformas embarcadas. A primeira versão permite a entrada de texto em vários campos de texto usando o teclado virtual, enquanto a segunda versão utiliza a mesma interface de usuário, mas com um teclado virtual personalizado do InputPanel. Os trechos de código a seguir mostram como configurar o projeto para escolher a implementação adequada com base nas opções do CONFIG:

  • No qmake (basic.pro):
    !qtConfig(vkb-desktop) {
        DEFINES += MAIN_QML=\\\"basic-b2qt.qml\\\"
    } else {
        DEFINES += MAIN_QML=\\\"Basic.qml\\\"
    }
  • No 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()

O exemplo habilita o teclado virtual definindo a variável de ambiente QT_IM_MODULE antes de carregar o arquivo .qml:

#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();
}

Além disso, ele utiliza itens personalizados TextField e TextArea para configurar o comportamento da tecla [ENTER] por meio da propriedade associada EnterKeyAction.

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)
                }
}

Os controles TextField e TextArea estendem os respectivos tipos de controlesQt Quick Controls 2 com as propriedades enterKeyEnabled e enterKeyAction. As instâncias TextField e TextArea no trecho de código podem definir essas propriedades para alterar o comportamento padrão.

Executando o exemplo

Você pode executar o exemplo a partir de:

Projeto de exemplo em code.qt.io

© 2026 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.