Search code examples
c++qtqmlqmakeqt6

How to add C++ QQuickPaintedItem in QML


I want to add C++ class like this notchedrectangle.hpp to QML:

#ifndef NOTCHEDRECTANGLE_HPP
#define NOTCHEDRECTANGLE_HPP

#include <QtQml/qqmlregistration.h>
#include <QQuickPaintedItem>

class NotchedRectangle : public QQuickPaintedItem
{
    Q_OBJECT
    Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
    QML_ELEMENT
public:
    NotchedRectangle();

    void paint(QPainter* painter) override;

    QColor color() const;
    void setColor(QColor color);

signals:
    void colorChanged();

private:
    QColor m_color;
};

#endif // NOTCHEDRECTANGLE_HPP

I have qmake build system, but don't know - what should I add in qmake file.

My filesystem looks like that:

enter image description here

I tried to add to qmake file this strings:

CONFIG += qmltypes
QML_IMPORT_NAME = UI.NR
QML_IMPORT_MAJOR_VERSION = 1
INCLUDEPATH += UI/NotchedRectangle

But they will cause error:

[Makefile.Debug:1175: qlauncher_metatypes.json] Error 1

Can you help me, please?


Solution

  • QML_IMPORT_NAME is not a name of class!

    It's the name of package and must be different.

    I use "Custom".

    Next you must include class in main.cpp

    And finally - you should make Recompile