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:
Qt Code:
  1. MyClass : public QObject
  2. {
  3. Q_OBJECT
  4.  
  5. Q_PROPERTY(QString myProperty1 READ myProperty1 WRITE setMyProperty1 NOTIFY myProperty1Changed)
  6. Q_PROPERTY(QString myProperty2 READ myProperty2 WRITE setMyProperty2 NOTIFY myProperty2Changed)
  7. //...
  8. };
To copy to clipboard, switch view to plain text mode 

MyClass.cpp:
Qt Code:
  1. void MyClass::setMyProperty1(QString arg)
  2. {
  3. Foo("myProperty1");
  4. }
  5.  
  6. void MyClass::setMyProperty2(QString arg)
  7. {
  8. Foo("myProperty2");
  9. }
To copy to clipboard, switch view to plain text mode 

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:
Qt Code:
  1. MyClass : public QObject
  2. {
  3. Q_OBJECT
  4.  
  5. Q_PROPERTY(QString myProperty1 READ myProperty1 WRITE setMyProperty NOTIFY myProperty1Changed)
  6. Q_PROPERTY(QString myProperty2 READ myProperty2 WRITE setMyProperty NOTIFY myProperty2Changed)
  7. //...
  8. };
To copy to clipboard, switch view to plain text mode 

MyClass.cpp:
Qt Code:
  1. void MyClass::setMyProperty(QString arg)
  2. {
  3. Foo(/* What goes in here? */);
  4. }
To copy to clipboard, switch view to plain text mode 

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!