Results 1 to 2 of 2

Thread: Casting QGraphicsItem

  1. #1
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Casting QGraphicsItem

    Hi everyone,

    I'm working on a charting application using graphicsview. In my scene I have two groups of items, main "project" items that the user can interact with (boxes and lines), and extraneous items that are either children of the main items, or display items like headers/footers.

    One of the common tasks I find myself doing is iterating through all the items in the scene and finding only the project items. For example, I have this type of code a lot:

    Qt Code:
    1. void ProjectScene::setSelectedItemsFont(QFont newFont) {
    2. QList<Sticky*> selectedNotes;
    3. foreach(QGraphicsItem* selectedItem, selectedItems()) {
    4. Sticky* item = dynamic_cast<Sticky*>(selectedItem);
    5. if(item) {
    6. selectedNotes.append(item);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Since items() returns every item, I need to find the ones that I'm interested in. Now I feel like dynamic_cast might not be the optimal way to do this (due to its inherent overhead). I've tried using QGraphicsItem::type() to find the correct items, but once I've identified which items I want, I still need to cast them to the right type to call the class-specific functions.

    So basically I guess the question is if there is a better way to do this. Once I've identified the type of an item using type(), is it ok to use static_cast instead?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Casting QGraphicsItem


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

    pherthyl (12th November 2007)

Similar Threads

  1. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  2. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  3. QGraphicsItem with QSizeGrip
    By elsheikhmh in forum Newbie
    Replies: 7
    Last Post: 19th November 2009, 18:53
  4. Deleting a scene from QGraphicsItem mouseEvent
    By JonathanForQT4 in forum Qt Programming
    Replies: 5
    Last Post: 10th April 2007, 11:27
  5. QGraphicsItem and signals
    By aamer4yu in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2006, 11:19

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.