3

I want to use qmllint on my Main.qml, which uses modules exposed from C++ side, such as the mainCtrls import and the mainControls keyword:

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Controls.Material 2.15

// My C++ module import
import mainCtrls 1.0

Window {
    id: root
    title: mainControls.version
    visible: true

    // more code...
}

The linter shows the following warnings:

Warning: Main.qml:4:1: Warnings occurred while importing module "mainCtrls": [import]
import mainCtrls
^^^^^^
---
Warning: Failed to import mainCtrls. Are your import paths set up properly? [import]
---

Warning: Main.qml:19:12: Unqualified access [unqualified]
    title: mainControls.version
           ^^^^^^^^^^^^
Warning: Main.qml:19:12: Could not compile binding for title: Cannot access value for name mainControls [compiler]
    title: mainControls.version

the mainCtrls module is a C++ class which is exposed in main.cpp using qmlRegisterType. The mainControls keyword can be used in QML due to setting a ContextProperty to the MainControlsModel class in my main.cpp:

int main(int argc, char *argv[])
{
    // ... some code ...

    qmlRegisterType<MainControlsModel>("mainCtrls", 1, 0, "MainModel");

    MainControlsModel mainControls;

    engine.rootContext()->setContextProperty("mainControls", &mainControls);

    // ... some more code ...

QObject::connect(
        &engine,
        &QQmlApplicationEngine::objectCreationFailed,
        &app,
        []() { QCoreApplication::exit(-1); },
        Qt::QueuedConnection);

    engine.loadFromModule("G6_GUIApp", "Main"); 

    // Start thread and Qt event loop (for frontend).
    backendThread.start();
    auto result = app.exec();

    // Thread clean up
    backendThread.quit();
    backendThread.wait();
    return result;
}

I know that the qmllint tool may not be able to statically link to the C++ module, hence, I have written a qmldir file and placed it into the directory, where my Main.qml lives:

// qmldir
module mainCtrls
typeinfo mainCtrls.qmltypes
classname MainModel

Additionally, I wrote a mainCtrls.qmltypes file and placed it in the same directory as the qmldir and Main.qml file:

[
    {
        "version": 1.0,
        "module": "mainCtrls",
        "name": "MainModel",
        "className": "MainControlsModel",
        "properties": [
            { // ...
            }
         ],
         "methods": [
            { // ...
            } 
         ],
         "signals": [
            { // ...
            }
         ],
    }
]

My CMakeLists.txt file includes the QML and source files:

qt_add_executable(My_GUIApp 
    src/main.cpp
    resources.qrc
)

qt_add_qml_module(My_GUIApp
    URI My_GUIApp
    VERSION 1.0

    SOURCES
        src/main.cpp 
        src/maincontrolsmodel.h src/maincontrolsmodel.cpp
        # more cpp files

    QML_FILES
        content/Main.qml
        # more QML files


    RESOURCES
        content/qmldir
        # more resources like svg's
)

My qmllint version is 6.6.2. I run the qmllint tool from commandline inside the directory, where qmldir, qmltypes and Main.qml live:

$ qmllint -i qmldir --dry-run Main.qml

What can I do to make it possible for qmllint to be aware of the C++ modules during static code analysis?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.