Results 1 to 6 of 6

Thread: about properties

  1. #1
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default about properties

    this might be a silly question, but still..
    taking the following example from the qt docs:
    Qt Code:
    1. class MyClass : public QObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
    5. Q_ENUMS(Priority)
    6.  
    7. public:
    8. MyClass(QObject *parent = 0);
    9. ~MyClass();
    10.  
    11. enum Priority { High, Low, VeryHigh, VeryLow };
    12.  
    13. void setPriority(Priority priority)
    14. {
    15. m_priority = priority;
    16. emit priorityChanged(priority);
    17. }
    18. Priority priority() const
    19. { return m_priority; }
    20.  
    21. signals:
    22. void priorityChanged(Priority);
    23.  
    24. private:
    25. Priority m_priority;
    26. };
    To copy to clipboard, switch view to plain text mode 
    I'm having some trouble understanding this: if the property is not dynamic, declared as a class member and i am responsible for emitting its signals and writing the methods it uses, who am i declaring Q_PROPERTY for? what are the advantages of predefined properties over dynamic ones?

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: about properties

    Without Q_PROPERTY you cannot use property related functions (in example QStateMachine, Animations, ...)

    Static properties are declared in the class interface so they are explicit for the class users.
    Last edited by mcosta; 21st July 2011 at 08:17. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: about properties

    i'm still kind of lost about this one.. a bit of a hypothetical question, but:
    say i have a class that holds a 3d object's properties (location, rotation, etc..) - when would or wouldn't you declare them static properties?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: about properties

    I think you don't understand what Q_PROPERTY does.
    Read here:
    http://doc.qt.nokia.com/latest/qobject.html#Q_PROPERTY
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jul 2010
    Posts
    63
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: about properties

    i do understand what it does, but not completely understand its purpose..

    my question became rather vague.. ill re-ask it:
    if a property is dynamic, it does makes sense in my head.
    if a property is not dynamic, and i need to write its READ, WRITE, NOTIFY etc.. myself, why would i declare it Q_PROPERTY? its already visible and accessible to clients - who or what require the Q_PROPERTY declaration? if i am using the class in pre-compile, i already see how to approach this class member.

    i'm probably missing something fundamental here..

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: about properties

    Properties behave like class data members, but they have additional features accessible through the Meta-Object System.
    For example, this way you export properties to Qt designer, to name one use.
    read more here:
    http://doc.qt.nokia.com/latest/prope...roperty-system
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QObject, properties
    By Archa4 in forum Newbie
    Replies: 8
    Last Post: 11th February 2011, 08:05
  2. dynamic properties
    By Hogwarts in forum Qt Programming
    Replies: 1
    Last Post: 3rd May 2010, 16:37
  3. Properties Window
    By manti_madhu in forum Qt Programming
    Replies: 3
    Last Post: 30th October 2009, 09:30
  4. gang together properties
    By khusmann in forum Qt Programming
    Replies: 2
    Last Post: 27th August 2008, 22:23
  5. ViewPort Properties
    By Kapil in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 11:57

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.