PDA

View Full Version : Problem with custom property binding (C++ extension)



vizzerdrix
17th October 2011, 14:07
Hi
I try to create a C++ extension for QML. I have one problem. I have an item (button) that can have set a custom gradient:

(I've pasted only important (in my opinion) code)



class Button : public QDeclarativeItem {
Q_OBJECT
Q_PROPERTY(Gradient* gradient READ gradient WRITE setGradient)

public:
Gradient* gradient() const;
void setGradient(Gradient* gradient);
};


QML file:



Button {
ExtendedGradient {
ExtendedGradientStop { position: "0, 0"; color: "red"; }
ExtendedGradientStop { position: "120, 120"; color: "blue"; }
}


and ExtendedGradient / ExtendedGradientStop classes:



class Gradient : public QObject
{
Q_OBJECT

Q_PROPERTY(QDeclarativeListProperty<GradientStop> stops READ stops)
Q_CLASSINFO("DefaultProperty", "stops")

public:
Gradient(QObject *parent=0);
virtual ~Gradient();

QDeclarativeListProperty<GradientStop> stops();
QGradient* gradient();

private:
QList<GradientStop *> stops_;
QGradient *gradient_;

friend class GradientStop;
};

class GradientStop : public QObject
{
Q_OBJECT

// properties and set/get methods
};

QML_DECLARE_TYPE(Gradient)
QML_DECLARE_TYPE(GradientStop)


Problem is that: In qml file I set a ExtendedGradient, but setter for this property is never called. Instead of this, it is set the dynamic property which have a name 'gradient' (the Gradient* Button::gradient_ is always 0x0).
This property isValid, !isNull, has correct typeName, but I cannot convert it to Gradient* (using property("gradient").value<Gradient*>()) - I always get 0x0.

Why the setGradient setter is not called? Is this possible to force a call of this method? What I'm doing wrong?