Results 1 to 5 of 5

Thread: Q_PROPERTY Code Generation

  1. #1
    Join Date
    Mar 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Q_PROPERTY Code Generation

    I am using the Q_PROPERTY macro as described here:
    http://doc.qt.nokia.com/4.7-snapshot/properties.html

    Let's consider this macro declaration:
    Q_PROPERTY(int foo READ getFoo WRITE setFoo)

    Now I am required to define a function getFoo() and setFoo(int):

    int getFoo() { return foo; }
    void setFoo(int x) { foo = x; }

    Is there a way to automatically generate these two functions? The reason I ask is I would like to define 20-30 Q_PROPERTY macros for each of the parameters that control a simulation system I coded up, and I never need to define a read or write function that's more complicated than the ones above.

    My only option right now is to write a Perl script to perform this code generation, but I was hoping there was an elegant way using Qt to specify "default functions" of this kind.

    Thanks.

  2. #2
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Q_PROPERTY Code Generation

    Sadly... you must write the getter and setter...

    in Objetive C you don't need...

    I don't know why Qt don't implement that.

  3. #3
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Q_PROPERTY Code Generation

    Why not making a macro?
    Qt Code:
    1. #define Q_PROPERTY_WITH_ACCESSORS(name, type, getter, setter) private: type _##name; public: Q_PROPERTY(type name READ getter WRITE setter) type const& getter () const { return _##name; } void setter (type const &v) { _##name = v; }
    To copy to clipboard, switch view to plain text mode 

    This macro will take 4 arguments:
    - Property's name,
    - Property's type,
    - Getter name,
    - Setter name.

    For example, you can call it where you would call Q_PROPERTY like that:
    Qt Code:
    1. Q_PROPERTY_WITH_ACCESSORS(toto, QString, toto, setToto)
    To copy to clipboard, switch view to plain text mode 
    It will generate a property named "toto" of type QString, reader "toto", setter "setToto", private member named "_toto".

    i did not test what would be the moc'ing result of that (Does MOC expands macros before processing?...) as i just invented it.

    Pierre.

  4. #4
    Join Date
    Sep 2013
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Q_PROPERTY Code Generation

    Asuming you have your private members, and you want public getters and setters :

    Qt Code:
    1. private :
    2. QString member1;
    3. QString member2;
    4. ]
    To copy to clipboard, switch view to plain text mode 

    right click on your member's name, in this case member 1 and/or member 2 (one at the time), then Refactor->Create Getters and Setters
    that will do it

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Q_PROPERTY Code Generation

    Well, this thread is three years old and Qt5 didn't exist back then, but there is a new variant of the macro now that can generate the getter and setter

    Qt Code:
    1. Q_PROPERTY(QColor color MEMBER m_color NOTIFY colorChanged)
    2. Q_PROPERTY(qreal spacing MEMBER m_spacing NOTIFY spacingChanged)
    3. Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged)
    4. ...
    5. signals:
    6. void colorChanged();
    7. void spacingChanged();
    8. void textChanged(const QString &newText);
    9.  
    10. private:
    11. QColor m_color;
    12. qreal m_spacing;
    13. QString m_text;
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 6
    Last Post: 26th April 2010, 17:47
  2. Why and when to use Q_PROPERTY
    By Vanir in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2007, 10:25
  3. How to Use Q_PROPERTY
    By mitesh_modi in forum Qt Programming
    Replies: 7
    Last Post: 20th June 2006, 15:49
  4. enlighten me on the use of Q_PROPERTY!!
    By nupul in forum Newbie
    Replies: 4
    Last Post: 3rd April 2006, 11:02
  5. Pointers and Q_PROPERTY
    By andersin in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2006, 15:05

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.