Results 1 to 7 of 7

Thread: protected QGraphicsObject

  1. #1
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default protected QGraphicsObject

    Hi!

    I want to create class that inherits QGraphicsObject but dont want public QGraphicsObject function to be accessible from my class.

    I can do the following:
    Qt Code:
    1. class MyObject : protected QGraphicsObject
    To copy to clipboard, switch view to plain text mode 
    But then I cant add object to a scene
    Qt Code:
    1. _scene->addItem(my_object);
    To copy to clipboard, switch view to plain text mode 

    Any advice?

    Thanks

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: protected QGraphicsObject

    It will not be directly possible to do so, as you have protected inheritance. The very point of having protected inheritance is to hide public functions, without exposing the public function of QGraphicsObject you will not be to use it / add it to any scene.

    Well there is a work around

    Qt Code:
    1. class MyObject : protected QGraphicsObject{};
    2.  
    3. MyObject my_object;
    4.  
    5. QGraphicsObject* gra_object= reinterpret_cast<QGraphicsObject*>(&my_object); // C++ style, static & dynamic cast will not work, as they obey strict type and visibility rules. dynamic cast overrides them
    6. //QGraphicsObject* gra_object= (QGraphicsObject*)(&my_object); // C Style, not recommended, but works
    7.  
    8. _scene->addItem(gra_object);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: protected QGraphicsObject

    Thanks for your answer! But I think I found more elegant solution...
    How about this?
    Qt Code:
    1. QGraphicsObject* MyObject::getBaseGraphicsObject(void)
    2. {
    3. return this;
    4. }
    To copy to clipboard, switch view to plain text mode 
    and then:
    Qt Code:
    1. _scene->addItem(my_object->getBaseGraphicsObject());
    To copy to clipboard, switch view to plain text mode 

    It is working, but im not sure if I could have other problems because of this?
    Last edited by stefan; 19th August 2011 at 05:08.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: protected QGraphicsObject

    Qt Code:
    1. _scene->addItem(my_object->getBaseGraphicsObject());
    To copy to clipboard, switch view to plain text mode 
    This one looks nice, and avoids odd looking cast operator . I don't think this will cause any other problems related to graphics scene/view, it should be good as long as the handle to QGraphicsObject* is available to scene.

  5. The following user says thank you to Santosh Reddy for this useful post:

    stefan (19th August 2011)

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: protected QGraphicsObject

    Aren't you exposing the graphics object interface to the public this way?
    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.


  7. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: protected QGraphicsObject

    wysota is right, this way graphics object interface is exposed as public, then making QGraphicsScene as a friend of MyObject should help.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: protected QGraphicsObject

    I think a better approach would be to hide the implementation completely and only expose the interface of MyObject to the world, like so:

    Qt Code:
    1. class MyObject {
    2. public:
    3. virtual void method1() = 0;
    4. virtual void method2() = 0;
    5. };
    6.  
    7. class MyObjectImpl : public QGraphicsObject, public MyObject {
    8. public:
    9. MyObjectImpl(QGraphicsItem *parent = 0) : QGraphicsObject(parent) {}
    10. void method1() { ... }
    11. void method2() { ... }
    12. };
    13.  
    14. class MyObjectFactory {
    15. public:
    16. MyObject *createMyObject() { return new MyObjectImpl(...); }
    17. };
    To copy to clipboard, switch view to plain text mode 
    You can declare the MyObjectImpl class in .cpp file to hide it completely from the developer and only expose MyObject interface. Of course if you don't allow outside world to create MyObject object, you don't need MyObjectFactory at all and can just instantiate implementation objects directly.

    An exercise to the reader: which two design patterns does this approach make use of?


    Added after 4 minutes:


    There is also this possibility:

    Qt Code:
    1. class MyObject {
    2. public:
    3. MyObject() { impl = new MyObjectImpl(this); }
    4. void method1() { impl->method1(); }
    5. void method2() { impl->method2(); }
    6. private:
    7. MyObjectImpl *impl;
    8. };
    9.  
    10. class MyObjectImpl : public QGraphicsObject {
    11. public:
    12. MyObjectImpl(MyObject *o), QGraphicsObject(), iface(o){}
    13. void method1() { ... }
    14. void method2() { ... }
    15. private:
    16. MyObject *iface;
    17. };
    To copy to clipboard, switch view to plain text mode 

    Again, what design pattern is that? I can give a hint that it makes more sense to use it if you want to subclass MyObject one day.
    Last edited by wysota; 19th August 2011 at 17: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. Replies: 1
    Last Post: 28th July 2011, 11:28
  2. Replies: 0
    Last Post: 5th January 2011, 18:17
  3. Virtual protected and private var
    By maddog_fr in forum Qt Programming
    Replies: 4
    Last Post: 19th November 2010, 16:14
  4. Replies: 1
    Last Post: 24th October 2010, 02:40
  5. About Password Protected Files:
    By vermarajeev in forum Qt Programming
    Replies: 7
    Last Post: 16th February 2007, 14:46

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.