Solved Custom type with properties like QML color
-
Hi all,
I can register custom type (not
QObject
derivative) and withQGadget
&Q_INVOKABLE
macro get access to its functions,
but
is there any way to have similar functionality likeQML 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_GADGET Q_PROPERTY(QString name READ name WRITE setName) Q_PROPERTY(int age READ age WRITE setAge) public: 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; } private: QString m_name; int m_age = 0; } Q_DECLARE_METATYPE(Pet)
-
@Wieland Thanks a million.
-
By the way,
Is there any difference for such a case//cpp Q_PROPERTY(QString name READ name WRITE setName) QString name() const { return m_name; } void setName(const QString &name) { m_name = name; } //QML property Pet pet pet.name
or
//cpp Q_INVOKABLE QString name() const { return m_name; } //QML property Pet pet pet.name()
I mean speed or memory consumption, which one is more preferable?
-
@SeeLook said in Custom type with properties like QML color:
Q_INVOKABLE
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 https://xmlgraphics.apache.org/fop/0.95/extensions.html
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...
read:
https://github.com/pehohlva/DocSpeacker/blob/master/3rdparty/oldtimer/FoColorName.cpp
https://github.com/pehohlva/DocSpeacker/blob/master/3rdparty/oldtimer/FoColorName.hall 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.