QML 정적 분석 1 - 기본 설정

이 장에서는 qmllint 확장 플러그인의 기본 구조와 qmllint와 함께 사용하는 방법을 소개합니다.

플러그인을 만들기 위해서는 먼저 QmlCompiler 모듈을 사용할 수 있도록 설정해야 합니다:

find_package(Qt6 REQUIRED COMPONENTS QmlCompiler)

그런 다음 플러그인을 생성하고, 이를 QmlCompiler 모듈에 연결합니다.

qt_add_plugin(HelloWorldPlugin)

target_sources(HelloWorldPlugin
    PRIVATE
        helloplugin.h
        helloplugin.cpp
)

target_link_libraries(HelloWorldPlugin PRIVATE Qt::QmlCompiler)

구현은 플러그인을 통해 Qt를 확장하는 패턴을 따릅니다. 즉, ` QQmlSA::LintPlugin`을 상속받아,

class HelloWorldPlugin : public QObject, public QQmlSA::LintPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID QmlLintPluginInterface_iid FILE "plugin.json")
    Q_INTERFACES(QQmlSA::LintPlugin)

public:
    void registerPasses(QQmlSA::PassManager *manager, const QQmlSA::Element &rootElement) override;
};

플러그인은 plugin.json 파일을 참조하며, 이 파일에는 다음과 같은 중요한 정보가 포함되어 있습니다:

{
    "name": "HelloWorld",
    "author": "Qt Example",
    "description": "Demonstrates how to write a qmllint plugin",
    "version": "1.0",
    "loggingCategories": [
        {
            "name": "hello-world",
            "settingsName": "HelloWorld",
            "description": "Used to create test messages"
        }
    ]
}

name, author, version 및 description 는 플러그인을 설명하는 메타데이터입니다.

각 플러그인은 하나 이상의 로깅 범주를 가질 수 있으며, 이는 경고를 주제별로 그룹화하는 데 사용됩니다. ` loggingCategories `는 범주 배열을 받아 처리하며, 각 범주는 다음과 같이 구성됩니다.

  • name로 구성되며, 이는 카테고리를 식별하는 데 사용되며 qmllint에서 플래그 이름으로 사용됩니다.
  • settingsName, 이는 qmllint.ini에서 카테고리를 구성하는 데 사용되며
  • description로, 이 항목은 해당 범주에 어떤 종류의 경고 메시지가 태그될지 설명해야 합니다.

이 예제에서는 ‘ hello-world ’라는 로깅 카테고리가 하나만 있습니다. 각 카테고리에 대해 플러그인 내에서 ` LoggerWarningId ` 객체를 생성해야 합니다.

static constexpr QQmlSA::LoggerWarningId helloWorld { "Plugin.HelloWorld.hello-world" };

이 객체는 Plugin.<plugin name>.<category name> 형식의 문자열 리터럴을 사용하여 생성합니다.

마지막으로, 플러그인이 실제로 유용한 작업을 수행하려면 registerPasses 함수를 구현해야 합니다.

void HelloWorldPlugin::registerPasses(QQmlSA::PassManager *manager, const QQmlSA::Element &rootElement)
{
    const bool pluginIsEnabled = manager->isCategoryEnabled(helloWorld);
    qDebug() << "Hello World plugin is" << (pluginIsEnabled ? "enabled" : "disabled");
   if (!pluginIsEnabled)
        return; // 플러그인이 비활성화된 경우 등록을 건너뜁니다
    // 나중에 여기서 패스를 등록할 것입니다
}

카테고리가 활성화되어 있는지 확인하고, ` qDebug`를 통해 상태를 나타내는 진단 메시지를 출력합니다. 현재는 패스를 등록하지 않았기 때문에 플러그인이 실제로는 아무런 작업도 수행하지 않는다는 점에 유의하세요. 이 작업은 이 튜토리얼의 다음 부분에서 진행될 예정입니다.

하지만 플러그인이 올바르게 감지되었는지 이미 확인할 수 있습니다. 다음 명령어를 사용하여 확인해 봅시다:

qmllint -P /path/to/the/directory/containing/the/plugin --Plugin.HelloWorld.hello-world info test.qml

-P 옵션은 qmllint가 지정된 폴더에서 플러그인을 찾도록 지시합니다. Qt 내부 카테고리와의 충돌을 피하기 위해, 플러그인 카테고리 이름은 항상 "Plugin"으로 시작하고, 그 뒤에 점(.), 플러그인 이름, 또 다른 점(.), 그리고 마지막으로 카테고리 순으로 구성됩니다. 이 명령을 실행하면 Hello World Plugin is enabled 이 출력되어야 합니다.

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