Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to add C++ QQuickPaintedItem in QML

How to add C++ QQuickPaintedItem in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qt6qml typesqml c++
4 Posts 3 Posters 826 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alrott SlimRG
    wrote on 31 Oct 2022, 11:08 last edited by
    #1

    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:

    alt text

    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

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bob64
      wrote on 31 Oct 2022, 15:08 last edited by
      #2

      I don't think you need to do anything special in your qmake file because it is a QQuickPaintedItem. Just add it as any other C++ class in your project.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Alrott SlimRG
        wrote on 31 Oct 2022, 22:46 last edited by
        #3

        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"
        }
        
        1 Reply Last reply
        0
        • C Offline
          C Offline
          Creaperdown
          wrote on 1 Nov 2022, 00:05 last edited by Creaperdown 11 Jan 2022, 00:08
          #4

          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

          1 Reply Last reply
          1

          3/4

          31 Oct 2022, 22:46

          • Login

          • Login or register to search.
          3 out of 4
          • First post
            3/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved