Results 1 to 6 of 6

Thread: One set slot for multiple Q_PROPERTYs

  1. #1
    Join Date
    Oct 2011
    Posts
    35
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default One set slot for multiple Q_PROPERTYs

    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!

  2. #2
    Join Date
    Oct 2011
    Posts
    35
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: One set slot for multiple Q_PROPERTYs

    Any thoughts? I'm really not sure how to get around this issue. I thought about using the QObject::sender() to get information, but I don't think that tells me what property triggered the call to the set method.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: One set slot for multiple Q_PROPERTYs

    You can't. How should that go?

  4. #4
    Join Date
    Oct 2011
    Posts
    35
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: One set slot for multiple Q_PROPERTYs

    So Qt doesn't provide functionality that allows me to know what property triggered a call to a certain set method? It seems like this would be useful for cutting down on a lot of similar code (particularly if you have a class that has dozens of Q_PROPERTYs).

  5. #5
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: One set slot for multiple Q_PROPERTYs

    1st of all setter pattern should look like this:
    Qt Code:
    1. void setSomeProp(const Type &x) {
    2. if (propertyField != x) {
    3. propertyField = x;
    4. somePropChanged(x);
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    so you don't signal change when noting changes (this also prevents recursion in many cases).

    2nd it is assumed that each property has own setter and getter so in coe you should know what property it is.
    If you want save on retyping you can define macro:
    Qt Code:
    1. #define DefStdSetterRef(_className_, _funcName_, _argType_, _propName_) \
    2. void _className_::_funcName_(const _argType_ &x) { \
    3. if (m##_propName_ != x) { \
    4. m##_propName_ = x; \
    5. _propName_##Changed(x); \
    6. } \
    7. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Oct 2011
    Posts
    35
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: One set slot for multiple Q_PROPERTYs

    Thanks MarekR2. In this case, I'm not following the generic setter method structure, as Foo in my case actually forwards the property setting over a network to be handled elsewhere (of course, the generic setter method structure is used in the resulting method call on the receiving end).

    You mentioned that "it is assumed that each property has own setter and getter." Is this just a coding convention or is this a requirement imposed by Qt? The macro you suggested does help, but it's still not as generic/clean of a solution as I am looking for (if one exists). I'd like to be able to only add a Q_PROPERTY in the .h file and not make any other method additions or changes to my .cpp file. This is what got me thinking about having all the Q_PROPERTYs in the class point to the same setter method. From a maintenance standpoint, this is cleaner and would cut down on redundant code, allowing all Q_PROPERTYs to be handled in a generic fashion. Is it safe to assume then that Qt does not provide the necessary information to make this scheme work (i.e., knowing what property change triggered the call to the setter method)?

Similar Threads

  1. Qt bug? Signal emitted once, slot called multiple times
    By MattPhillips in forum Qt Programming
    Replies: 22
    Last Post: 1st December 2010, 22:32
  2. Replies: 2
    Last Post: 12th June 2010, 02:21
  3. Multiple Signal in a slot
    By QAlex in forum Qt Programming
    Replies: 2
    Last Post: 20th November 2009, 11:39
  4. Single slot for multiple list boxes
    By Sheetal in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2008, 06:53
  5. Replies: 3
    Last Post: 11th January 2008, 17:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.