Results 1 to 4 of 4

Thread: Inheriting from QGraphicsItem: getting pointer to custom class fromselectedItems()

  1. #1
    Join Date
    May 2013
    Posts
    13
    Thanks
    4

    Default Inheriting from QGraphicsItem: getting pointer to custom class fromselectedItems()

    Hello,

    This might be a c++ question rather than qt.

    I have written a custom class which inherits from QGraphicsPolygonItem and have populated a scene with this kind of object. Now I wish to know when one of them is selected, so I 've connected the scene's selectionChanged() signal to a custom slot. In that slot function, the scene's selectedItems() will give me a QList<QGraphicsItem*>, ie a list of the items selected. However, I want a qlist of my custom class.
    From what I read on casting base class to derived, dynamic_cast could only work if QGraphicsItem was polymorphic. Trying static_cast instead caused a seg fault. Am I right to assume that the QList returned by selectedItems() contains what I want and it is a matter of casting? Should I be approaching this in an entirely different manner? (although I do not want to turn my custom class into a QObject to use its signals)

    Any advice or ideas would be appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Inheriting from QGraphicsItem: getting pointer to custom class fromselectedItems(

    dynamic_cast should work, it is there for exactly that purpose.

    It will return the pointer casted to your class or 0 if the object is not of the cast type.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Durkin (13th June 2013)

  4. #3
    Join Date
    May 2013
    Posts
    13
    Thanks
    4

    Default Re: Inheriting from QGraphicsItem: getting pointer to custom class fromselectedItems(

    You 're right, it does. The seg fault was my mistake.

  5. #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: Inheriting from QGraphicsItem: getting pointer to custom class fromselectedItems(

    If you have implemented the QGraphicsItem:type() in your custom class, then you can directly use the type() to determine the custom items, it should be more simpler and quicker than using dynamic_cast.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    Durkin (14th June 2013)

Similar Threads

  1. Inheriting Custom Decorated Dialog
    By dcrespo in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2012, 13:18
  2. Replies: 3
    Last Post: 31st July 2011, 00:30
  3. Replies: 1
    Last Post: 7th March 2011, 14:02
  4. Replies: 2
    Last Post: 21st April 2010, 08:07
  5. QListWidget inheriting custom class
    By phannent in forum Qt Tools
    Replies: 1
    Last Post: 4th August 2008, 14:39

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.