Results 1 to 4 of 4

Thread: Pointers and Q_PROPERTY

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pointers and Q_PROPERTY

    Quote Originally Posted by andersin
    can this be done at all?
    To some extent:
    Qt Code:
    1. #include <QtDebug>
    2.  
    3. #include <QCoreApplication>
    4. #include <QObject>
    5. #include <QVariant>
    6.  
    7. class BInterface
    8. {
    9. public:
    10. virtual int foo() = 0;
    11. };
    12.  
    13. class B : public QObject, public BInterface
    14. {
    15. Q_OBJECT
    16. Q_PROPERTY( int b READ b WRITE setB )
    17. public:
    18.  
    19. virtual int foo() { return mv_b; }
    20.  
    21. const int b() const { return mv_b; }
    22. void setB( int b ) { mv_b = b; }
    23.  
    24. private:
    25. int mv_b;
    26. };
    27.  
    28. class A : public QObject
    29. {
    30. Q_OBJECT
    31. Q_PROPERTY( QObject* B READ B WRITE setB )
    32. public:
    33. A( QObject *b = 0 ) : mv_B( b ) {}
    34. virtual ~A() { if(mv_B) delete mv_B; }
    35. QObject * B() { return mv_B; }
    36. void setB( QObject* B ) { if (mv_B) delete mv_B; mv_B = B; }
    37.  
    38. private:
    39. QObject* mv_B;
    40. };
    41.  
    42. int main( int argc, char **argv )
    43. {
    44. QCoreApplication app( argc, argv );
    45.  
    46. QObject *a = new A( new B() );
    47.  
    48. // QObject *a = new A();
    49. // a->setProperty( "B", 0 ); // works
    50.  
    51. // QObject *a = new A();
    52. // a->setProperty( "B", new B() ); // crashes
    53.  
    54. QObject* b1 = a->property( "B" ).value< QObject *>();
    55. if( b1 != 0 ) {
    56. b1->setProperty( "b", 1234 );
    57. }
    58. else {
    59. qDebug() << "b1 is null";
    60. }
    61.  
    62. QObject* b2 = a->property( "B" ).value< QObject *>();
    63. if( b2 != 0 ) {
    64. qDebug() << b2->property( "b" );
    65. }
    66. else {
    67. qDebug() << "b2 is null";
    68. }
    69.  
    70. return 0;
    71. }
    72.  
    73. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    Output:
    $ ./properties
    QVariant(int, 1234)

  2. The following user says thank you to jacek for this useful post:

    andersin (28th February 2006)

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.