Results 1 to 5 of 5

Thread: Q_PROPERTY Code Generation

Hybrid View

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

    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.

  2. #2
    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

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

    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, 16:47
  2. Why and when to use Q_PROPERTY
    By Vanir in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2007, 09:25
  3. How to Use Q_PROPERTY
    By mitesh_modi in forum Qt Programming
    Replies: 7
    Last Post: 20th June 2006, 14:49
  4. enlighten me on the use of Q_PROPERTY!!
    By nupul in forum Newbie
    Replies: 4
    Last Post: 3rd April 2006, 10:02
  5. Pointers and Q_PROPERTY
    By andersin in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2006, 14: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
  •  
Qt is a trademark of The Qt Company.