QMLアプリケーションのビルド

「C++ コンソールアプリケーションのビルド」では、単純なコンソールアプリケーション用の CMakeLists.txt ファイルを紹介しました。ここでは、 Qt Quick モジュールを使用するQMLアプリケーションを作成します。

以下にプロジェクトファイルの全文を示します:

cmake_minimum_required(VERSION 3.16)

project(helloworld VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 6.5 COMPONENTS Quick REQUIRED)

qt_standard_project_setup(REQUIRES 6.5)

qt_add_executable(helloworld
    main.cpp
)

qt_add_qml_module(helloworld
    URI hello
    QML_FILES
        main.qml
        FramedImage.qml
    RESOURCES
        img/world.png
)

target_link_libraries(helloworld PRIVATE Qt6::Quick)

コンソールアプリケーション(CMakeLists.txt )と比較して、どのような変更を加えたのか順を追って見ていきましょう:

    ...
find_package(Qt6 6.5 COMPONENTS Quick REQUIRED)

find_package() の呼び出しにおいて、Core をQuick に置き換えています。これにより、CMakeはQt6Quick モジュールを読み込み、後でリンク対象となるQt6::Quick ターゲットを提供します。また、CMakeはQuickが依存するパッケージ(Qt6Qml など)も自動的に読み込みます。 には、後述するqt_add_qml_module()コマンドが定義されています。

また、Qt 6.5 以降も必要です。

qt_standard_project_setup(REQUIRES 6.5)

qt_standard_project_setup()コマンドは、一般的な Qt アプリケーション向けにプロジェクト全体のデフォルト設定を指定します。REQUIRES 6.5 を追加することで、qt_add_qml_module() によって作成される QML モジュールのデフォルトのリソースプレフィックスを定義するポリシーQTP0001 を有効にします。

    ...
qt_add_qml_module(helloworld
    URI hello
    QML_FILES
        main.qml
        FramedImage.qml
    RESOURCES
        img/world.png
)

qt_add_qml_module() は、hello という名前の QML モジュールを実行可能ファイルに追加します。このモジュールは、2 つの QML ファイルと 1 つの画像で構成されています。QTP0001 により、このモジュールはリソースファイルシステム内のqrc:/qt/qml/hello で利用可能となり、qrc:/qt/qml/ は QMLエンジンのデフォルトのインポートパスの 1 つとなります。

qt_add_qml_module() は、QMLスクリプトコンパイラの実行や、helloworld_qmllint ターゲットの定義といった最適化処理も行います。このターゲットを実行することで、qmllint から.qml 内のファイルに関する追加の提案を得ることができます。

target_link_libraries(helloworld PRIVATE Qt6::Quick)

target_link_libraries() コマンドでは、実行ファイルをQt6::Quick にリンクします。これにより、Qt6::Quick が依存するターゲット(Qt6::Qml やQt6::Core など)にも自動的にリンクされます。

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