Results 1 to 2 of 2

Thread: Querying object type using itemAt

  1. #1
    Join Date
    May 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Querying object type using itemAt

    I'm writing an application that uses QGraphicsScene and subclassed QGraphicsItems, based on the Diagram Scene demo app. The user can click on the scene and select an item, which can be either a DiagramItem (which inherits QGraphicsPolygonItem), or an Arrow (which inherits QGraphicsLineItem).

    I need to distinguish the class that the selected object belongs to, but I'm not sure how to do it. Below is the relevant code from my subclassed QGraphicsScene class (DiagramScene):

    Qt Code:
    1. void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. if (itemAt(event->scenePos())) {
    4.  
    5. DiagramItem *selectedItem = qgraphicsitem_cast<DiagramItem *>(itemAt(event->scenePos()));
    6. // operate on selectedItem
    To copy to clipboard, switch view to plain text mode 

    How can I modify this to check whether I've selected a DiagramItem or an Arrow, and then process selectedItem accordingly? At the moment the app crashes when I click on an Arrow object. (I thought about using exception handling, but am not sure how you try and catch casting errors.)

    Any advice would be greatly appreciated!

  2. #2
    Join Date
    May 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Querying object type using itemAt

    Ah, figured out a solution - qgraphicsitem_cast returns a zero if the cast doesn't work, so can try and recast it using the other class type if that's the case.

Similar Threads

  1. [solved] Which object type is QObject::sender()?
    By ricardo in forum Qt Programming
    Replies: 6
    Last Post: 8th May 2009, 21:03
  2. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43
  3. Getting type of object
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 29th November 2007, 14:38
  4. dummy question(Error)
    By Masih in forum Qt Programming
    Replies: 12
    Last Post: 19th July 2007, 23:38
  5. Replies: 3
    Last Post: 15th April 2007, 19:16

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.