Results 1 to 4 of 4

Thread: Best way to cast QList of base class pointers to derived class pointers

  1. #1
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Best way to cast QList of base class pointers to derived class pointers

    I have a QList of base class pointers:

    Qt Code:
    1. QList<QGraphicsItem*> selectedItems = myGraphicsScene->selectedItems();
    To copy to clipboard, switch view to plain text mode 

    but I'd like it to be a derived class, e.g., QList<DerivedGraphicsItem*>. Is there any way to do this other than iterating through the list and creating a new one?

    Note that in this particular case, I want to static cast, although I'm curious how it would work to dynamic cast in this situation too.

  2. #2
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Best way to cast QList of base class pointers to derived class pointers

    As far as I know, neither static nor dynamic cast will do that. QList<DerivedGraphicsItem*> is not a subclass of QList<GraphicsItem*>, moreover, QList is a template class, that means it could be specialized differently for different template paremeters, so it is not legal to cast. If you explain why do you need to cast it, maybe I can propose a solution.

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

    wayfaerer (6th April 2012)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Best way to cast QList of base class pointers to derived class pointers

    You will have to build a second list, casting each base class pointer to the derived class pointer, and discarding those that do not cast. If they are QGraphicsItem objects then you can use qgraphicsitem_cast. I would not use static_cast unless you can guarantee all the items in the scene are of the one type, and even then...

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

    wayfaerer (6th April 2012)

  6. #4
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Re: Best way to cast QList of base class pointers to derived class pointers

    My QGraphicsScene consists of only one type of selectable item (but other types of non-selectable items), so it's certain that any selected items will be of that type, so static_cast should be okay.

    It's not a big deal, I'm just asking because whenever I try to operate on the selected items, I have to cast in a bunch of places, and it's a little messy.

Similar Threads

  1. Replies: 2
    Last Post: 22nd November 2011, 05:19
  2. Replies: 8
    Last Post: 15th July 2010, 21:42
  3. Replies: 5
    Last Post: 20th April 2010, 17:48
  4. Can not cast into the derived class from QScrollBar
    By learning_qt in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2008, 10:23
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

Tags for this Thread

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.