Results 1 to 2 of 2

Thread: Casting of a QGraphicsItem subclass

  1. #1
    Join Date
    Sep 2016
    Posts
    8
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Casting of a QGraphicsItem subclass

    Hello,
    i'm new in QT and i'm having problems with QGraphicsItem and my suclassed "Item".
    I have a QGraphicsView that contains a QGraphicsScene in which i'm inserting some items.
    Those items are a subclass of QGraphicsItem and i have called it TestHoverItem (It contains some additional values).

    If i try to get one of the object i have in the QGraphicsScene using the itemAt() function i have no problem (code below)
    Qt Code:
    1. QGraphicsItem::mousePressEvent(e);
    2. QPoint point = e->scenePos().toPoint();
    3.  
    4. auto sceneTop = scene();
    5.  
    6. QGraphicsItem *item = sceneTop->itemAt(point.x(), point.y(),QTransform());
    7. TestHoverItem *hover = qgraphicsitem_cast<TestHoverItem *>(item);
    8. qDebug() << "Codice " << QString::number(hover->getCodice());
    To copy to clipboard, switch view to plain text mode 

    If i try to get all the objects from the QGraphicsScene using the items() function the application crashes

    Qt Code:
    1. QList<QGraphicsItem *> items;
    2. items = sceneTop->items();
    3. qDebug() << "Numero items " + QString::number(items.size());
    4. //Fin qui tutto bene
    5. TestHoverItem * aa= new TestHoverItem();
    6. for(int i=0; i<items.size(); i++){
    7. aa =qgraphicsitem_cast<TestHoverItem *>(items[i]);
    8. qDebug() << "Codice: " +QString::number(aa->getCodice());
    9. }
    To copy to clipboard, switch view to plain text mode 

    Can someone explain me what i'm doing wrong?
    Thank you for the time and , i hope , for the answers

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Casting of a QGraphicsItem subclass

    TestHoverItem * aa= new TestHoverItem();

    Why do you do this? You need a variable to hold a pointer to a TestHoverItem, not a new item.

    aa =qgraphicsitem_cast<TestHoverItem *>(items[i]);
    If the item is -not- a TestHoverItem, then the cast returns a NULL pointer. You then proceed to use that pointer (without checking to see if it -is- NULL) in the qDebug() statement that follows.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QGraphicsItem ambiguity in subclass
    By Talei in forum Qt Programming
    Replies: 1
    Last Post: 7th April 2014, 10:53
  2. own subclass of QGraphicsItem problem
    By Azras in forum Newbie
    Replies: 1
    Last Post: 30th May 2013, 16:10
  3. Problem with QGraphicsItem Casting
    By D Cell in forum Newbie
    Replies: 1
    Last Post: 9th April 2010, 20:11
  4. Casting QGraphicsItem child from QGraphicsItem
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 29th August 2008, 16:37
  5. Casting QGraphicsItem
    By pherthyl in forum Qt Programming
    Replies: 1
    Last Post: 12th November 2007, 00:56

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.