View Full Version : Problem with QObject::setProperty

8th September 2011, 16: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_PROPERTY(Color tulipColor READ tulipColor WRITE setTulipColor)

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

Color tulipColor() const;

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:

// ........

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();


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 :/

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

9th September 2011, 04: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:

12th September 2011, 11:17
Help anyone ?