2

I've got a property of a custom type.

class foo : public QObject {
    Q_OBJECT
    Q_PROPERTY(Custom x READ x WRITE set_x)

public: 
    void set_x(Custom &x) { /*whatnot*/}
}

QJson effectively invokes the following dynamic assignment:

((QObject*)&foo_instance)->setProperty("x", QVariant(QString("something-from-json")))

which returns false, as documented in the Qt:

If the value is not compatible with the property's type, the property is not changed, and false is returned.

How can I shim this into my Custom value? Clearly defining a side-function void set_x(QString) or void set_x(QVariant) cannot possibly work, since the property system would be unaware of this accessor.

Also, where is the type compatibility checked? - the program control never reaches

int foo::qt_metacall(QMetaObject::Call _c, int _id, void **_a)

the function generated by the meta-object compiler..

How can I make Custom compatible with these types?

qdot
  • 6,195
  • 5
  • 44
  • 95

1 Answers1

2

It can be useful to read The Property System

Especially Properties and Custom Types part.

If to be brief, all properties save as QVariant.
You need to use Q_DECLARE_METATYPE macro to make your Custom type compatible with Qt properties.
And after that you have to call qRegisterMetaType function to register your type.

aleks_misyuk
  • 567
  • 1
  • 4
  • 16
  • 1
    Yes, I've read this part of the documentation - the problem starts with defining the WRITE accessor - it needs to be of type Custom (or Custom& or Custom*, IIRC) - but I'm getting the setProperty dynamic caller to shove QVariant(QString).value() into there. There is a Custom(QString) creator, but I can't get the setProperty to invoke it either statically or dynamically. And declaring the WRITE accessor typed set_x(QVariant &v) doesn't help either - it's not being called. – qdot Sep 23 '12 at 20:32
  • 2
    If I'm not mistaken, you have only one way to do it - use QString property. And if you need you can convert QString to Custom type into setter. – aleks_misyuk Sep 23 '12 at 21:57