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 https://bugreports.qt.io/secure/attachment/146411/untitled1.zip in case you want to fully reproduce but first some samples of what i think it's important


QML FILE

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_OBJECT
    QML_ELEMENT  
  
    Q_PROPERTY(QAbstractItemModel* model READ model WRITE setModel NOTIFY modelChanged)

public:
    explicit ObjectWithModel(QObject* parent = nullptr);  

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

signals:
    void modelChanged();

private:
    QAbstractItemModel* mModel  = nullptr;
};

CMAKE FILE

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

qt_add_executable(appuntitled1 main.cpp)

qt_add_qml_module(appuntitled1
    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?

2 comments:

Anonymous said...

I don't know anything about qmllint but is it something to do with the use of AbstractItemModel* model vs QAbstractItemModel in the header?

Albert Astals Cid said...

AbstractItemModel* model vs QAbstractItemModel in the header is a typo, my code has QAbstractItemModel*.

Found a "fix", posted it over in reddit. https://www.reddit.com/r/QtFramework/comments/16tjm1g/comment/k2how5a/?utm_source=share&utm_medium=web2x&context=3