Custom type with properties like QML color

  • Hi all,

    I can register custom type (not QObject derivative) and with QGadget & Q_INVOKABLE macro get access to its functions,
    is there any way to have similar functionality like QML color, to have property-like access:

    property color someColor: "red"
    // then we have access to its "props" like:
    someColor.r // red component
    someCalor.a // alpha

    Is it possible for custom type which has to be light like in this example QColor/QML color is?

  • Hi! Yes that's possible:

    class Pet
        Q_PROPERTY(QString name READ name WRITE setName)
        Q_PROPERTY(int age READ age WRITE setAge)
        QString name() const { return m_name; }
        void setName(const QString &name) { m_name = name; }
        int age() const { return m_age; }
        void setAge(int age) { m_age = age; }
        QString m_name;
        int m_age = 0;

  • @Wieland Thanks a million.

  • By the way,
    Is there any difference for such a case

    Q_PROPERTY(QString name READ name WRITE setName)
    QString name() const { return m_name; }
    void setName(const QString &name) { m_name = name; }
    property Pet pet


    Q_INVOKABLE QString name() const { return m_name; }
    property Pet pet

    I mean speed or memory consumption, which one is more preferable?

  • @SeeLook said in Custom type with properties like QML color:


    It's both correct and useful code. The only difference is that if you add Q_INVOKABLE then that function is also registered to the meta object system / can also be called from QML. Speed: don't know, sry.

  • Long time a go i register all standard xsl:fo color same color from openoffice or apache fop xml

    i register as FopColor:FopColor

    record("black" 	,QColor( 0, 0, 0,255));
    	record("blanchedalmond"	,QColor(255, 235, 205,255));
    	record("blue"	,QColor( 0, 0, 255,255));
    	record("blueviolet" 	,QColor(138, 43, 226,255));
    	record("brown"	,QColor(165, 42, 42,255));
    	record("burlywood"	,QColor(222, 184, 135,255));
    	record("cadetblue"	,QColor( 95, 158, 160,255));
    	record("chartreuse"	,QColor(127, 255, 0,255));
    	record("chocolate"	,QColor(210, 105, 30,255));
    	record("coral" 	,QColor(255, 127, 80,255));
    	record("cornflowerblue"	,QColor(100, 149, 237,255));
    	record("cornsilk" 	,QColor(255, 248, 220,255));
    	record("crimson"	,QColor(220, 20, 60,255));
    	record("cyan"	,QColor( 0, 255, 255,255));
           and many moore...


    all this color you find inside openoffice libreoffice format to read odt file ... qt can write odt file (but not read) i suppose now in qt5 have moore specification...
    inside Okular source you find out the same color specification.. the list is very long...

  • Thank You for the answers.
    It clarified steps to take in my code.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.