You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求提供CMake中引入QtVirtualKeyboard模块的示例

How to Include Qt VirtualKeyboard in Your CMake Project

Got it, let's walk through exactly how to set up QtVirtualKeyboard in your CMake project so you can access its classes without issues. Here's a straightforward example and breakdown:

Step 1: Basic CMakeLists.txt Structure

First, here's a minimal, complete CMakeLists.txt that properly pulls in the Qt Virtual Keyboard module. I'll cover both Qt 6 and Qt 5 since support varies by version:

For Qt 6:

cmake_minimum_required(VERSION 3.24)
project(MyVirtualKeyboardApp)

# Enable Qt's standard project setup (recommended for Qt 6)
qt_standard_project_setup()

# Find Qt 6 with required components, including VirtualKeyboard
find_package(Qt6 REQUIRED COMPONENTS Core Widgets VirtualKeyboard)

# Add your executable (replace with your target name and source files)
add_executable(MyVirtualKeyboardApp
    main.cpp
    mainwindow.cpp
    mainwindow.h
)

# Link Qt libraries to your target, including VirtualKeyboard
target_link_libraries(MyVirtualKeyboardApp PRIVATE
    Qt6::Core
    Qt6::Widgets
    Qt6::VirtualKeyboard
)

# Optional: If you need to deploy the keyboard (for release builds)
qt_generate_deploy_app_script(
    TARGET MyVirtualKeyboardApp
    OUTPUT_SCRIPT deploy_script
    NO_UNSUPPORTED_PLATFORM_ERROR
)
install(SCRIPT ${deploy_script})

For Qt 5:

If you're still using Qt 5, the setup is slightly different:

cmake_minimum_required(VERSION 3.10)
project(MyVirtualKeyboardApp)

# Set C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find Qt 5 with required components
find_package(Qt5 REQUIRED COMPONENTS Core Widgets VirtualKeyboard)

# Add executable
add_executable(MyVirtualKeyboardApp
    main.cpp
    mainwindow.cpp
    mainwindow.h
)

# Link Qt libraries
target_link_libraries(MyVirtualKeyboardApp PRIVATE
    Qt5::Core
    Qt5::Widgets
    Qt5::VirtualKeyboard
)

# For Qt 5, explicitly enable the module's resources
qt5_use_modules(MyVirtualKeyboardApp Core Widgets VirtualKeyboard)

Step 2: Accessing QtVirtualKeyboard Classes in Your Code

Once the CMake setup is done, you can include and use the module's classes directly. For example, to access the input context (a common use case):

#include <QApplication>
#include <QVirtualKeyboardInputContext>

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

    // Enable the virtual keyboard (required for it to appear)
    qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));

    // Access the input context class
    auto inputContext = QVirtualKeyboardInputContext::instance();
    if (inputContext) {
        // Example: Connect to a signal from the input context
        QObject::connect(inputContext, &QVirtualKeyboardInputContext::inputMethodChanged,
                         [](const QString &inputMethod) {
                             qDebug() << "Input method changed to:" << inputMethod;
                         });
    }

    // Rest of your app setup...
    return app.exec();
}

Key Notes

  • Verify Qt Installation: Make sure your Qt installation includes the VirtualKeyboard module. Some default installs skip it—you can check via Qt Maintenance Tool and add it if missing.
  • Environment Variable: For the keyboard to actually appear in your app, you need to set QT_IM_MODULE to qtvirtualkeyboard (as shown in the code example). This tells Qt to use the virtual keyboard input method.
  • Qt Version Differences: Qt 6 uses qt_standard_project_setup() which simplifies boilerplate, while Qt 5 relies on qt5_use_modules() to properly link module resources.

内容的提问来源于stack exchange,提问作者kpo8

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:43:08