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:
Q_OBJECT
Q_PROPERTY(Color tulipColor READ tulipColor WRITE setTulipColor)
tlp::Color _color;
public:
explicit ColorButton
(QWidget *parent
= 0);
Color tulipColor() const;
protected:
public slots:
void setTulipColor(const 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&);
};
To copy to clipboard, switch view to plain text mode
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");
Q_DECLARE_METATYPE(tlp::Color)
// ........
qRegisterMetaType<tlp::Color>("Color");
To copy to clipboard, switch view to plain text mode
Then I use the following code:
qWarning() << "User Type for value is: " << value.userType();
ColorButton* btn = new ColorButton();
for (int i=0;i<btn->metaObject()->propertyCount();++i) {
qWarning() << "UserType for the " << prop.name() << " property is: " << prop.userType();
}
btn
->setProperty
("tulipColor",
QVariant::fromValue<tlp
::Color>
(Color
(255,
0,
0)));
btn->show();
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();
To copy to clipboard, switch view to plain text mode
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 :/
Bookmarks