- Qt 6.8
- Qt Qml
- QML Lint Warning and Errors
- Missing type
- Qt 6.8.2 Reference Documentation

# Missing type

This warning category is spelled `[missing-type]` by qmllint.

## Cannot deduce type of alias

### What happened?

An alias property points to a property with a C++ type whose QML counterpart was not found. This can be caused by importing a QML module which do not declare its QML dependencies on other modules.

Note: If you are importing QML modules with external dependencies, verify that they are actually installed, and that their modules end up in an import path.

The warning might also indicate that the type of the property referenced by the alias does not have a QML counterpart. The referenced property type might be missing the QML_ELEMENT macro, for example. Refer to Defining QML Types from C++ or Overview - QML and C++ Integration in this case.

### Why is this bad?

QML tooling is not able to find the QML counterpart of the C++ type: the compiler can't compile this property alias to C++ and qmllint as well as QML Language Server can't analyze this property alias.

### Example

Let our QML module have one C++ class with a property `myProperty`:

     #include <QQuickItem>
     #include <QtQml/qqmlregistration.h>
     #include <QObject>

     class MyCppObject : public QObject
     {
         Q_OBJECT
         QML_ELEMENT
     public:
         MyCppObject(QObject *parent = nullptr)
             : QObject(parent)
         {}

         Q_PROPERTY(QQuickItem *myProperty READ myProperty WRITE setMyProperty NOTIFY notifyMyProperty)
         QQuickItem *myProperty() { return m_myProperty; }
         void setMyProperty(QQuickItem *item) { emit notifyMyProperty(); m_myProperty = item; }

     private:
         QQuickItem *m_myProperty;

     signals:
         void notifyMyProperty();
     };

with following `CMakeLists.txt`:

     project(mymodule VERSION 0.1 LANGUAGES CXX)

     set(CMAKE_CXX_STANDARD_REQUIRED ON)
     find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
     qt_standard_project_setup(REQUIRES 6.5)

     qt_add_executable(appmymodule
         main.cpp
     )

     qt_add_qml_module(appmymodule
         URI mymodule
         VERSION 1.0
         QML_FILES Main.qml HelloWorld.qml
         SOURCES mycppobject.cpp mycppobject.h
     )

     target_link_libraries(appmymodule
      PRIVATE Qt6::Quick
     )

The C++ dependency `Quick` was declared, such that this class can compile and the QQuickItem include can be found. Also, `mymodule` does not have any dependency on QtQuick.

Now, let's try to use `myProperty` in an alias in QML. The program will run but QML tooling like the compiler, for example, will complain about the usage of `myProperty`:

     import mymodule

     MyCppObject {
         id: root

         property alias myAlias: root.myProperty // not ok: Cannot deduce type of alias [missing-type]
     }

The reason for the warning message is that in the QML code, the type `QQuickItem` of `myProperty` and its QML counterpart `Item` are not known, even if you have `import QtQuick` in your QML file. This is because the same type can be exposed multiple times with different attributes in different modules: `mymodule` actually has to be precise about the QML type of `myProperty`.

To fix this warning, add the dependency in the `CMakeLists.txt`:

     qt_add_qml_module(mymodule
         URI mymodule
         ...
         # declarare QML dependency to QtQuick module
         DEPENDENCIES QtQuick
         ...
     )

Now, the warning should be gone!

See also Declaring module dependencies.

## No type found for property

### What happened?

A binding was set on a property whose QML type was not found. This can be caused by a QML module which does not declare its QML dependencies on other modules.

Note: If you are importing QML modules with external dependencies, verify that they are actually installed, and that their modules end up in an import path.

The warning might also indicate that the type of the property does not have a QML counterpart. The property type might be missing the QML_ELEMENT macro, for example. Refer to Defining QML Types from C++ or Overview - QML and C++ Integration in this case.

### Why is this bad?

QML tooling is not able to find the QML counterpart of the C++ type: the compiler can't compile this property binding to C++ and qmllint as well as QML Language Server can't analyze this property binding.

### Example

Let our QML module have a C++ class with two properties, `myProperty` and `myProperty2`:

     #include <QQuickItem>
     #include <QtQml/qqmlregistration.h>
     #include <QObject>

     class MyCppObject : public QObject
     {
         Q_OBJECT
         QML_ELEMENT
     public:
         MyCppObject(QObject *parent = nullptr)
          : QObject(parent)
         {}

         Q_PROPERTY(QQuickItem *myProperty READ myProperty WRITE setMyProperty NOTIFY notifyMyProperty)
         QQuickItem *myProperty() { return m_myProperty; }
         void setMyProperty(QQuickItem *item) { emit notifyMyProperty(); m_myProperty = item; }

         Q_PROPERTY(QQuickItem *myProperty2 READ myProperty2 WRITE setMyProperty2 NOTIFY notifyMyProperty2)
         QQuickItem *myProperty2() { return m_myProperty2; }
         void setMyProperty2(QQuickItem *item) { emit notifyMyProperty2(); m_myProperty2 = item; }

     private:
         QQuickItem *m_myProperty;
         QQuickItem *m_myProperty2;

     signals:
         void notifyMyProperty();
         void notifyMyProperty2();
     };

with following `CMakeLists.txt`:

     project(mymodule VERSION 0.1 LANGUAGES CXX)

     set(CMAKE_CXX_STANDARD_REQUIRED ON)
     find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
     qt_standard_project_setup(REQUIRES 6.5)

     qt_add_executable(appmymodule
         main.cpp
     )

     qt_add_qml_module(appmymodule
         URI mymodule
         VERSION 1.0
         QML_FILES Main.qml HelloWorld.qml
         SOURCES mycppobject.cpp mycppobject.h
     )

     target_link_libraries(appmymodule
      PRIVATE Qt6::Quick
     )

The C++ dependency `Quick` was declared, such that this class can compile and the QQuickItem include can be found. Also, `mymodule` does not have any dependency on QtQuick.

Now, let's try to bind `myProperty2` to `myProperty` in an alias in QML. The program will run but QML tooling like the compiler, for example, will complain about the usage of `myProperty`:

     import mymodule

     MyCppObject {
         id: root

         myProperty: myProperty2 // not ok: No type found for property "myProperty". [missing-type]
     }

The reason for the warning message is that in the QML code, the type `QQuickItem` of `myProperty` and its QML counterpart `Item` are not known: the dependency 'QtQuick' of mymodule was not declared in the `CMakeLists.txt`.

To fix this warning, add the dependency in the `CMakeLists.txt`:

     qt_add_qml_module(mymodule
         URI mymodule
         ...
         # declarare QML dependency to QtQuick module
         DEPENDENCIES QtQuick
         ...
     )

Now, the warning should be gone!

See also Declaring module dependencies.