PDA

View Full Version : Problem with QObject::setProperty



Auliyaa
8th September 2011, 15:16
Hi everyone,

I'm currently working on a library that defines several types and I want to be able to use them into the Qt's property system.

For instance, I have a ColorButton class containing a "tulipColor" field whose type is tlp::Color:


class TLP_QT_SCOPE ColorButton : public QPushButton {
Q_OBJECT
Q_PROPERTY(Color tulipColor READ tulipColor WRITE setTulipColor)

tlp::Color _color;
public:
explicit ColorButton(QWidget *parent = 0);

Color tulipColor() const;

protected:
void paintEvent(QPaintEvent *);

public slots:
void setTulipColor(const Color&);
};

I declared the tlp::Color type using the Q_DECLARE_METATYPE macro and the qRegisterMetaType function:


Q_DECLARE_METATYPE(tlp::Color)
// ........
qRegisterMetaType<tlp::Color>("Color");

Then I use the following code:

QVariant value = QVariant::fromValue<tlp::Color>(tlp::Color(255,0,0));
qWarning() << "User Type for value is: " << value.userType();

ColorButton* btn = new ColorButton();
for (int i=0;i<btn->metaObject()->propertyCount();++i) {
QMetaProperty prop = btn->metaObject()->property(i);
qWarning() << "UserType for the " << prop.name() << " property is: " << prop.userType();
}

btn->setProperty("tulipColor",QVariant::fromValue<tlp::Color>(Color(255,0,0)));
btn->show();

And get the following output:


User Type for value is: 258
UserType for the tulipColor property is: 258

So the tulipColor property is correctly registered with a valid userType. But when I try to change the value using the setProperty method, it returns false and my setTulipColor method is never called.

Please help :/

wysota
8th September 2011, 15:55
What if you change "Color" to "tlp::Color"?

Auliyaa
9th September 2011, 03:07
Using namespace names in Q_PROPERT declarations doesn't sem to compile, I get the same error as related in this bug report: https://bugreports.qt.nokia.com//browse/QTBUG-2151

That's why I tried using Color since my class is in the same namespace. Now if I change qRegisterMetaType<tlp::Color>("Color"); to qRegisterMetaType<tlp::Color>("tlp::Color"); the output of my program becomes:

User Type for value is: 258
UserType for the tulipColor property is: 0

I'm really lost on this one :confused:

Auliyaa
12th September 2011, 10:17
Help anyone ?