Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Exposing struct members to QML from derived QQuickItem-class



  • Hiall

    I have a class deriving from QQuickItem with overloaded updatePaintNode among others ...
    The class is registered as qmlRegisterType<DPShip>("dpship", 1, 0, "DPShip");
    This works perfectly fine.

    I want to have border.width and border.color properties in QML for my class.
    So in DPShip.h I tried:

    struct borderStruct
    {
        Q_GADGET
        Q_PROPERTY(qreal width MEMBER m_width)
        Q_PROPERTY(QColor color MEMBER m_color)
        
        qreal m_width;
        QColor m_color;
    };
    
    class DPShip : public QQuickItem
    {
        Q_OBJECT
        Q_PROPERTY(borderStruct border READ border WRITE setBorder NOTIFY borderChanged)
    
    public:
        DPShip(QQuickItem *parent = Q_NULLPTR);
    
    protected:
        QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData);
    
        borderStruct border() const;
        setBorder(const borderStruct& border);
    
    private:
        borderStruct border_obj;
    
    signals:
        void borderChanged(const borderStruct &border);
    }
    

    In QML I instantiate my item as DPShip { }, which works fine as mentioned above, also Qt Creator recognizes the border property, but no members, ie. in the editor I get the error: "border" does not have members. (M17) when I write border.width for instance.

    How do I do this?


Log in to reply