Home > Software engineering >  How to add C QQuickPaintedItem in QML
How to add C QQuickPaintedItem in QML

Time:11-02

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?

CodePudding user response:

you should import in qml file after exposing the class:

import UI.NR 1.0

I suggest you to change UI.NR to smth else which has no dot in it.

Then take an instance:

WhatEverYouNamed {

}

CodePudding user response:

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

  • Related