Results 1 to 12 of 12

Thread: Const-Correctness With Q_PROPERTY

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2010
    Posts
    14
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1

    Default Const-Correctness With Q_PROPERTY

    I'm trying to develop a class that will be used in a reflection driven application and I ran into an interesting issue that I'm hoping there is a solution to. Let's say I have a class Dummy that has a few properties, one of which is a pointer to another Dummy object:
    Qt Code:
    1. class Dummy : public QObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(int id READ id WRITE setId)
    5. Q_PROPERTY(QObject * child READ child)
    6.  
    7. int myId;
    8. DummyObject *myChild;
    9.  
    10. public:
    11.  
    12. Dummy(int id)
    13. : myId(id), myChild(0)
    14. {
    15. if (myId == 0)
    16. {
    17. myChild = new Dummy(myId + 1);
    18. }
    19. }
    20.  
    21. ~Dummy()
    22. {
    23. delete myChild;
    24. }
    25.  
    26. int id() const
    27. {
    28. return myId;
    29. }
    30.  
    31. void setId(int id)
    32. {
    33. myId = id;
    34. }
    35.  
    36. QObject * child()
    37. {
    38. return myChild;
    39. }
    40.  
    41. };
    To copy to clipboard, switch view to plain text mode 

    And then lets say I create a new Dummy object with id = 0 (so it has one child with id = 1), if I pass the parent Dummy object to a function that takes a constant reference to a QObject (or a constant pointer for that matter), how can I guarantee const-correctness on the child function? I tried the obvious: Q_PROPERTY(const QObject * child READ child) and then making const QObject * child() const (which is really how I want this function to look like), but then the moc complains about: "Unable to handle unregistered datatype 'const QObject*'". I just can't figure out how to make it so that if I pass a constant reference or pointer to a Dummy object that the "child" function (through the qt property system) will expose a constant reference or pointer (to ensure const-correctness).

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

    Default Re: Const-Correctness With Q_PROPERTY

    The property name and type and the READ function are required. The type can be any type supported by QVariant, or it can be a user-defined type.
    Now look at enum QVariant::Type.
    QObject * is not there.

    Then:
    Custom types used by properties need to be registered using the Q_DECLARE_METATYPE() macro so that their values can be stored in QVariant objects. This makes them suitable for use with both static properties declared using the Q_PROPERTY() macro in class definitions and dynamic properties created at run-time.
    have a look at QVariant qVariantFromValue ( const T & value );.

    One easy way would be to typedef a long as pointer type and convert it in your class back to pointer again.
    ==========================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.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Const-Correctness With Q_PROPERTY

    I'd say that trying to have a static property that holds QObject pointers is a bad idea without even dwelling about whether it works or not. Qt has another facility for holding hierarchies of objects that you should be using here. If you want to hold some reference to the child object then have that property contain a unique id to the child object that can be used to retrieve the real object pointer for the id.

    Something like this should work:
    Qt Code:
    1. class MyObject : public QObject {
    2. Q_OBJECT
    3. Q_PROPERTY(int id READ id WRITE setId)
    4. Q_PROPERTY(int childId READ childId WRITE setChildId)
    5. public:
    6. // ...
    7. void setId(int id) {
    8. if(m_id == id) return;
    9. if(ObjectManager::instance()->hasId(m_id)) {
    10. ObjectManager::instance()->unregister(m_id);
    11. }
    12. if(ObjectManager::instance()->register(id, this)){
    13. m_id = id;
    14. } else {
    15. qWarning("Id %d already taken. Operation failed.", id);
    16. }
    17. }
    18. void setChildId(int id) {
    19. if(id == m_childId) return;
    20. if(!ObjectManager::instance()->hasId(id)){
    21. qWarning("No object with id %d exists. Operation failed.", id);
    22. return;
    23. }
    24. m_childId = id;
    25. }
    26. };
    27.  
    28. // ...
    29.  
    30. QObject* ObjectManager::objectById(int id) const {
    31. return m_objects.value(id, 0);
    32. }
    To copy to clipboard, switch view to plain text mode 

    provided ObjectManager is a singleton handling registration of objects in the system.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 4th December 2009, 18:03
  2. const member and const method
    By mickey in forum General Programming
    Replies: 8
    Last Post: 9th April 2008, 10:44
  3. Why and when to use Q_PROPERTY
    By Vanir in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2007, 10:25
  4. How to Use Q_PROPERTY
    By mitesh_modi in forum Qt Programming
    Replies: 7
    Last Post: 20th June 2006, 15:49
  5. Pointers and Q_PROPERTY
    By andersin in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2006, 15:05

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.