How to add C++ QQuickPaintedItem in QML
Unsolved
QML and Qt Quick
-
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:
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?
P.S. All project you can find here: GitHub
-
And how can I use it in QML?
it writes - unknown type, when I write:NotchedRectangle { id: subtract anchors{ bottom: parent.bottom left: leftRoundedCorner.right right: parent.right } color: "#ffffff" }
-
I am pretty sure you need to register it to the qmlEngine via
qmlRegisterType<NotchedRectangle>("My.Items", 1, 0, "NotchedRectangle");
In your main.cpp, then you should be able to use it. (https://www.qt.io/blog/qml-type-registration-in-qt-5.15)
In your qml file you can then just type:
import My.Items
and then you should be able to use
NotchedRectangle