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);
};
class Button : public QDeclarativeItem {
Q_OBJECT
Q_PROPERTY(Gradient* gradient READ gradient WRITE setGradient)
public:
Gradient* gradient() const;
void setGradient(Gradient* gradient);
};
To copy to clipboard, switch view to plain text mode
QML file:
Button {
ExtendedGradient {
ExtendedGradientStop { position: "0, 0"; color: "red"; }
ExtendedGradientStop { position: "120, 120"; color: "blue"; }
}
Button {
ExtendedGradient {
ExtendedGradientStop { position: "0, 0"; color: "red"; }
ExtendedGradientStop { position: "120, 120"; color: "blue"; }
}
To copy to clipboard, switch view to plain text mode
and ExtendedGradient / ExtendedGradientStop classes:
{
Q_OBJECT
Q_PROPERTY(QDeclarativeListProperty<GradientStop> stops READ stops)
Q_CLASSINFO("DefaultProperty", "stops")
public:
virtual ~Gradient();
QDeclarativeListProperty<GradientStop> stops();
private:
QList<GradientStop *> stops_;
friend class GradientStop;
};
class GradientStop
: public QObject{
Q_OBJECT
// properties and set/get methods
};
QML_DECLARE_TYPE(Gradient)
QML_DECLARE_TYPE(GradientStop)
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)
To copy to clipboard, switch view to plain text mode
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?
Bookmarks