fonzi337
21st February 2012, 23:32
Hi,
I'm trying to find a good solution for a maintenance problem I have with a class that declares a number of QString Q_PROPERTYs. Basically, I have five Q_PROPERTYs defined in a class each with their own set method for the WRITE. Each set method looks nearly identical: it calls a function Foo passing in the name of the property. Thus, I get something that looks like this:
MyClass.h:
MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(QString myProperty1 READ myProperty1 WRITE setMyProperty1 NOTIFY myProperty1Changed)
Q_PROPERTY(QString myProperty2 READ myProperty2 WRITE setMyProperty2 NOTIFY myProperty2Changed)
//...
};
MyClass.cpp:
void MyClass::setMyProperty1(QString arg)
{
Foo("myProperty1");
}
void MyClass::setMyProperty2(QString arg)
{
Foo("myProperty2");
}
I'd like some generic way to ensure that as I add new Q_PROPERTYs to this class that I don't have to keep defining these separate boiler-plate set methods.
One idea I have is to have all the properties map to the same set method, as shown below:
MyClass.h:
MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(QString myProperty1 READ myProperty1 WRITE setMyProperty NOTIFY myProperty1Changed)
Q_PROPERTY(QString myProperty2 READ myProperty2 WRITE setMyProperty NOTIFY myProperty2Changed)
//...
};
MyClass.cpp:
void MyClass::setMyProperty(QString arg)
{
Foo(/* What goes in here? */);
}
The problem I'm having is figuring out how to determine what particular Q_PROPERTY triggered the call to setMyProperty so I can call Foo supplying it with the correct string.
Does Qt provide any facilities to make this work in such a generic fashion?
Thanks!
I'm trying to find a good solution for a maintenance problem I have with a class that declares a number of QString Q_PROPERTYs. Basically, I have five Q_PROPERTYs defined in a class each with their own set method for the WRITE. Each set method looks nearly identical: it calls a function Foo passing in the name of the property. Thus, I get something that looks like this:
MyClass.h:
MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(QString myProperty1 READ myProperty1 WRITE setMyProperty1 NOTIFY myProperty1Changed)
Q_PROPERTY(QString myProperty2 READ myProperty2 WRITE setMyProperty2 NOTIFY myProperty2Changed)
//...
};
MyClass.cpp:
void MyClass::setMyProperty1(QString arg)
{
Foo("myProperty1");
}
void MyClass::setMyProperty2(QString arg)
{
Foo("myProperty2");
}
I'd like some generic way to ensure that as I add new Q_PROPERTYs to this class that I don't have to keep defining these separate boiler-plate set methods.
One idea I have is to have all the properties map to the same set method, as shown below:
MyClass.h:
MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(QString myProperty1 READ myProperty1 WRITE setMyProperty NOTIFY myProperty1Changed)
Q_PROPERTY(QString myProperty2 READ myProperty2 WRITE setMyProperty NOTIFY myProperty2Changed)
//...
};
MyClass.cpp:
void MyClass::setMyProperty(QString arg)
{
Foo(/* What goes in here? */);
}
The problem I'm having is figuring out how to determine what particular Q_PROPERTY triggered the call to setMyProperty so I can call Foo supplying it with the correct string.
Does Qt provide any facilities to make this work in such a generic fashion?
Thanks!