Results 1 to 8 of 8

Thread: How to get a pointer to a QObject's property ?

  1. #1
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default How to get a pointer to a QObject's property ?

    Assuming here's a class named QXXX inherited from QObject, how can i get the address of the property ["Abc"], any idea?


    Qt Code:
    1. class QABC : public QObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(int Val READ Val WRITE setVal)
    5. public:
    6. QABC (QObject* parent = 0, int a = 987) : QObject(parent)
    7. {
    8. setVal(a);
    9. }
    10. ~QABC ()
    11. {
    12. }
    13. QABC (const QABC & rhs)
    14. {
    15. copyFrom(rhs);
    16. }
    17.  
    18. QABC & copyFrom(const QABC & rhs)
    19. {
    20. if(this == &rhs)
    21. return *this;
    22.  
    23. setVal(rhs._val);
    24.  
    25. return *this;
    26. }
    27.  
    28. QABC & operator = (const QABC & rhs)
    29. {
    30. return copyFrom(rhs);
    31. }
    32.  
    33. int Val() const { return _val; }
    34. void setVal( int val ) { _val = val; }
    35. private:
    36. int _val;
    37. };
    38. class QXXX : public QObject
    39. {
    40. Q_OBJECT
    41. Q_PROPERTY(QABC Abc READ Abc WRITE setAbc)
    42. public:
    43. QXXX(QObject* parent = 0, QABC a = QABC()) : QObject(parent)
    44. {
    45. setAbc(a);
    46. }
    47. ~QXXX()
    48. {
    49. }
    50. QXXX(const QXXX& rhs)
    51. {
    52. copyFrom(rhs);
    53. }
    54.  
    55. QXXX& copyFrom(const QXXX& rhs)
    56. {
    57. if(this == &rhs)
    58. return *this;
    59.  
    60. setAbc(rhs._Abc);
    61.  
    62. return *this;
    63. }
    64.  
    65. QXXX& operator = (const QXXX& rhs)
    66. {
    67. return copyFrom(rhs);
    68. }
    69.  
    70. QAbc Abc() const { return _Abc; }
    71. void setAbc( QAbc val ) { _Abc= val; }
    72. private:
    73. QABC _Abc;
    74. };
    To copy to clipboard, switch view to plain text mode 


    QXXX x;
    ...
    QObject * z = (QObject*)x;
    QObject* pointer = z ...?? ???? // to get (&QXXX::_Abc)
    I just only know the property name, it's 'Abc', can i get &z->_Abc?
    Last edited by HiJack; 27th August 2010 at 05:51.

  2. #2
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default Re: How to get a pointer to a QObject's property ?

    somebody ...

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get a pointer to a QObject's property ?


  4. #4
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default Re: How to get a pointer to a QObject's property ?

    Quote Originally Posted by aamer4yu View Post
    Man do you mean z->property("Abc") ?? No, I want to get the member's address. For example, I got a QObject* pointer, even I'm not sure whether it has a propery named 'Abc'. If it has,
    then give me back its address, please. It should be a QObject* pointer.
    something like this.

  5. #5
    Join Date
    Jul 2010
    Posts
    21
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get a pointer to a QObject's property ?

    I got a QObject* pointer, even I'm not sure whether it has a propery named 'Abc'. If it has,
    then give me back its address, please.
    Did you check the source? See QObject:roperty() and QMetaProperty::read() for the dirty details.

  6. #6
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default Re: How to get a pointer to a QObject's property ?

    Quote Originally Posted by hobbyist View Post
    Did you check the source? See QObject:roperty() and QMetaProperty::read() for the dirty details.
    Ya I do have seen back to QMetaProperty, but thers's no way to help me to get the member's address unless you modify the moc file. ( seems like anti-oop, hur? )
    I can enumerate the children list of one QObject object, but first I must set each member's parent, and second there's no one child pointer if the member's type is int, or QString, bool, etc. so I gave up this way.
    Another suggestion?
    Last edited by HiJack; 28th August 2010 at 06:54.

  7. #7
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default Re: How to get a pointer to a QObject's property ?

    solved manually. you guys.

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

    Default Re: How to get a pointer to a QObject's property ?

    Properties are not QObjects and don't have addresses. I think your whole concept is flawed because of a wrong assumption. If you'd like to get an address to a value of a property, it still wouldn't have worked as QVariant (which stores the value) is implicitly shared and thus the place in memory holding the value may vary in time (and you'd get a stale pointer). If you want to have a property which is QObject then that's another flaw as QVariant values are mutable and QObjects are not. You should only store a pointer to a QObject within a property.

    Qt Code:
    1. class PropVal : public QObject {
    2. Q_PROPERTY(int val READ ... WRITE ...);
    3. // ...
    4. };
    5.  
    6. class ABC : public QObject {
    7. Q_PROPERTY(QObject* abc READ ... WRITE ...);
    8. // ...
    9. };
    10. // ...
    11. ABC abc;
    12. PropVal *prop = qobject_cast<PropVal*>(qvariant_cast<QObject*>(abc.property("abc")));
    13. if(prop) {
    14. int val = prop->property("val").toInt();
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 28th August 2010 at 11:04.
    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. C++ readonly property
    By yyiu002 in forum Newbie
    Replies: 16
    Last Post: 22nd June 2010, 10:26
  2. How to retrieve the device property?
    By vjsharma_30 in forum Qt Programming
    Replies: 0
    Last Post: 24th February 2010, 18:24
  3. Replies: 1
    Last Post: 31st October 2007, 14:14
  4. enum property
    By illuzioner in forum Qt Tools
    Replies: 10
    Last Post: 22nd August 2006, 21:47
  5. AlignCenter property of the QTableView???
    By manhds in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2006, 09:35

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.