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 は、プラグインを記述するためのメタデータです。
各プラグインは、警告をテーマ別にグループ化するために使用される1つ以上のロギングカテゴリを持つことができます。loggingCategories はカテゴリの配列を受け取り、各カテゴリは
nameで構成され、カテゴリを識別するために使用されます。また、qmllint でのフラグ名として、settingsName、これは qmllint.ini 内でカテゴリを設定するために使用されdescription。これは、そのカテゴリにタグ付けされる警告メッセージの種類を記述するものです。
この例では、ロギングカテゴリは「hello-world 」の1つだけです。各カテゴリについて、プラグイン内でLoggerWarningId オブジェクトを作成する必要があります。
static constexpr QQmlSA::LoggerWarningId helloWorld { "Plugin.HelloWorld.hello-world" };これは、「Plugin.<plugin name>.<category name> 」という形式の文字列リテラルを使用して構築します。
最後に、プラグインが実際に何か有用な処理を行うためには、registerPasses 関数を実装する必要があります。
voidHelloWorldPlugin::registerPasses(QQmlSA::PassManager*manager, constQQmlSA::Element &rootElement)
{
const boolpluginIsEnabled= 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.