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

C++ コンソールアプリケーションのビルドでは、単純なコンソールアプリケーションの CMakeLists.txt ファイルを示しました。ここでは、このモジュールを使った QML アプリケーションを作成します。 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() の呼び出しで、CoreQuick に置き換えています。そのため、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()は、2つのQMLファイルと1つのイメージからなるhello というQMLモジュールを実行ファイルに追加します。QTP0001により、このモジュールはリソースファイルシステムのqrc:/qt/qml/helloqrc:/qt/qml/ はQMLエンジンのデフォルトインポートパスのひとつです。

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

target_link_libraries(helloworld PRIVATE Qt6::Quick)

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

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