Qml Module not found CPP Class registration with new QML_ELEMENT r

Viewed 1384

I try to register my custom CPP Class in QML with Qt5.15.0 and the new macro QML_ELEMENT, but the Module is not found. The Qt Creater help files describes the steps for the QML_ELEMENT. I checked the Qt Manual too, but without happy end (https://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html).

//testclass.h

#ifndef TESTCLASS_H
#define TESTCLASS_H

#include <QObject>
#include <QtQml>

class testclass : public QObject
{
    Q_OBJECT
    QML_ELEMENT

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

signals:

};

#endif // TESTCLASS_H


// testclass.cpp
#include "testclass.h"

testclass::testclass(QObject *parent) : QObject(parent)
{

}

The pro file looks like this:

QT += quick

CONFIG += c++11

CONFIG += qmltypes
QML_IMPORT_NAME = com.mycompany.test
QML_IMPORT_MAJOR_VERSION = 1

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp \
        testclass.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += \
    testclass.h

The Metafile gets generated with this content:

[
    {
        "classes": [
            {
                "classInfos": [
                    {
                        "name": "QML.Element",
                        "value": "auto"
                    }
                ],
                "className": "testclass",
                "object": true,
                "qualifiedClassName": "testclass",
                "superClasses": [
                    {
                        "access": "public",
                        "name": "QObject"
                    }
                ]
            }
        ],
        "inputFile": "testclass.h",
        "outputRevision": 67
    }
]

And afterwards I try to use this class in my Main.qml, but Qt Creator shows QML Module not found.

// main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import com.mycompany.test 1.0 // QML Module not found

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")


}

//Main.cpp
#include "testclass.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

Did I missed something or is it buggy?

** Edit **

I refactored the Classname from testclass to Testclass, but still I get the same error. Of Course I made Clean and rebuild. I'm using Windows 10, MinGw-64Bit and qmake.

//testclass.h
#ifndef TESTCLASS_H
#define TESTCLASS_H

#include <QObject>
#include <QtQml>

class Testclass : public QObject
{
    Q_OBJECT
    QML_ELEMENT

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

signals:

};

#endif // TESTCLASS_H

//testclass.cpp

#include "testclass.h"

Testclass::Testclass(QObject *parent) : QObject(parent)
{

}

// Metafile
[
    {
        "classes": [
            {
                "classInfos": [
                    {
                        "name": "QML.Element",
                        "value": "auto"
                    }
                ],
                "className": "Testclass",
                "object": true,
                "qualifiedClassName": "Testclass",
                "superClasses": [
                    {
                        "access": "public",
                        "name": "QObject"
                    }
                ]
            }
        ],
        "inputFile": "testclass.h",
        "outputRevision": 67
    }
]

//main.cpp
#include "testclass.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

//main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import com.mycompany.test 1.0

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")


    

}

Qml error

2 Answers

I got the same error with the example code trying to build it with MinGW. But the strange thing is that the same code works flawless after compiling for Android with Clang multi-Abi.

you could try using the older methode of doing this from QT 5.9 https://doc.qt.io/qt-5.9/qtqml-cppintegration-topic.html

you just need to add this in you main.cpp and include your Testclass header

qmlRegisterType<Testclass>("com.mycompany.test", 1, 0, "Testclass")

Although I don't get that error if I get the following warning:

Invalid QML element name "testclass"; type names must begin with an uppercase letter

And since when using QML_ELEMENT the name of the class is taking as the name of the item and the QML rules the names of the items must start with capital letters so it causes an error. The solution is to change the class name from testclass to Testclass, then recompile (it may be necessary to remove the build folder).

Related