Results 1 to 7 of 7

Thread: protected QGraphicsObject

Hybrid View

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

    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
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    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
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 6 Times in 6 Posts

    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
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    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,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    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.

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
  •  
Qt is a trademark of The Qt Company.