Results 1 to 5 of 5

Thread: QGraphicsItem subclass and accessing custom properties

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QGraphicsItem subclass and accessing custom properties

    I created several subclasses of QGraphicsItem's and added them to the scene.
    How can I access their custom properties added by me using [...] ?
    Qt Code:
    1. scene.itemAt(x_number).myproperty
    To copy to clipboard, switch view to plain text mode 

    If I try creating a new placeholder subclass instance, and assigning scene.itemAt(x) to it, I receive an error: "QGraphicsItem is private"

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QGraphicsItem subclass and accessing custom properties

    Quote Originally Posted by been_1990 View Post
    If I try creating a new placeholder subclass instance, and assigning scene.itemAt(x) to it, I receive an error: "QGraphicsItem is private"
    Make it public?

  3. #3
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsItem subclass and accessing custom properties

    My problem is similar to here.
    In the above thread, Wysota says:
    casting that listbox item to your class
    And that was the solution, but I have no idea how to do that.
    In my case it would be casting a QGraphicsScene item to my subclass.

  4. #4
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem subclass and accessing custom properties

    Hi,

    itemAt() returns a pointer to a QGraphicsItem. So the returned value is a (QGraphicsItem *) and not a (YourSubClass *). If the return object is your own subclass (and not some other object that is also in the graphics scene) and you want to use its specific methods, you must first cast it to your own class.

    See int QGraphicsItem::type () and T qgraphicsitem_cast ( QGraphicsItem * item ) in the documentation for a possible solution. It is used in the diagramscene example with Qt.

    Regards,
    Marc

  5. #5
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsItem subclass and accessing custom properties

    Ok, thanks Marc I went straight to the Docs and was able to work it out.

Similar Threads

  1. QT4 custom designer plugin properties ignored
    By Intangir in forum Qt Programming
    Replies: 7
    Last Post: 23rd May 2015, 11:56
  2. Exposing custom widget properties
    By Plixil in forum Newbie
    Replies: 1
    Last Post: 25th July 2010, 16:38
  3. QWizard fields and custom properties
    By HelLViS69 in forum Qt Programming
    Replies: 1
    Last Post: 4th May 2010, 16:41
  4. QGraphicsItem subclass access to QGraphicsView size
    By rubenvb in forum Qt Programming
    Replies: 4
    Last Post: 23rd January 2010, 21:36
  5. accessing subclass of QListBoxText
    By jcsneaks in forum Newbie
    Replies: 9
    Last Post: 7th February 2006, 07:28

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.