Wednesday, September 27, 2023

Am I using qmllint wrong? Or is it still not there?

Today I was doing some experiments with qmllint hoping it would help us make QML code more robust.

I created a very simple test which is basically a single QML file that creates an instance of an object I've created from C++.

But when running qmllint via the all_qmllint  target it tells me

Warning: Main.qml:14:9: No type found for property "model". This may be due to a missing import statement or incomplete qmltypes files. [missing-type]
        model: null
Warning: Main.qml:14:16: Cannot assign literal of type null to QAbstractItemModel [incompatible-type]
        model: null

Which is a relatively confusing error, since it first says that it doesn't know what the model property is, but then says "the model property is an QAbstractItemModel and you can't assign null to it"

Here the full code in case you want to fully reproduce but first some samples of what i think it's important


import QtQuick
import QtQuick.Window

import untitled1 // This is the name of my import

Window {
    // things     
    ObjectWithModel {
        model: null

HEADER FILE (there's nothing interesting in the cpp file)

#pragma once

#include <QtQmlIntegration>
#include <QAbstractItemModel>
#include <QObject>

class ObjectWithModel : public QObject {
    Q_PROPERTY(QAbstractItemModel* model READ model WRITE setModel NOTIFY modelChanged)

    explicit ObjectWithModel(QObject* parent = nullptr);  

    AbstractItemModel* model() const;
    void setModel(QAbstractItemModel* model);

    void modelChanged();

    QAbstractItemModel* mModel  = nullptr;


cmake_minimum_required(VERSION 3.16)
project(untitled1 VERSION 0.1 LANGUAGES CXX)
find_package(Qt6 6.4 REQUIRED COMPONENTS Quick)

qt_add_executable(appuntitled1 main.cpp)

    URI untitled1 VERSION 1.0
    QML_FILES Main.qml
    SOURCES ObjectWithModel.h ObjectWithModel.cpp

target_link_libraries(appuntitled1 PRIVATE Qt6::Quick)  

As you can see it's quite simple and, as far as I know, using the recommended way of setting up a QML module when using a standalone app.


But maybe I am holding it wrong?