Note that there are some explanatory texts on larger screens.

plurals
  1. POQT 4.5 - QGraphicsItem is assigned to parent scene of 0x0
    primarykey
    data
    text
    <p>This is a rather strange situation here. I have a class which derived from QGraphicsScene</p> <pre><code>class DrawingScene : public QGraphicsScene { ... } </code></pre> <p>Whenever I add any item within the derived class, the item ended up attaching to a parent of 0x0. The thing is that despite that, the graphics item appearing within the scene as normal, and can be interacted with. For instance, this snippet of code:</p> <pre><code>QGraphicsEllipseItem * newEllipse = addEllipse(rect, pen); qDebug() &lt;&lt; "Scene info " &lt;&lt; this newEllipse-&gt;setPos(pos); newEllipse-&gt;setZValue(100); qDebug() &lt;&lt; "New point added = " &lt;&lt; newEllipse; </code></pre> <p>Here are some depug output</p> <pre><code>Scene info DrawingScene(0x1d2ffb8) New point added = QGraphicsItem(this = 0x3f0fda0 , parent = 0x0 , pos = QPointF(326, 307), z =100, flags = {"isVisible|isEnabled" }) </code></pre> <p>And whenever I try to delete the item, I get a warning that the scene is different, but the item is gone nonetheless.</p> <p>Am I missing something, or is QGraphicsScene not meant to be derived from in the first place?</p> <p><strong>Edit: Code for deletion</strong></p> <p>The QGraphicsItems, are added to QMap for fast mapping purpose and to disable a group of them at a go. So this is how the item is added to the scene (as above) and the Map</p> <pre><code>int SceneTrackerData::addLink(QGraphicsLineItem * newLink) { links_.insert(currentLinkID_, newLink); qDebug() &lt;&lt; links_; // Store the ID of the newly added link and increment link count int thisLinkID = currentLinkID_; currentLinkID_++; newLink-&gt;setData(LinkData::LinkID, QVariant(thisLinkID)); qDebug() &lt;&lt; "Link " &lt;&lt; thisLinkID &lt;&lt; " connecting " &lt;&lt; newLink-&gt;data(0).toInt() &lt;&lt; " and " &lt;&lt; newLink-&gt;data(1).toInt() &lt;&lt; " added."; return thisLinkID; } </code></pre> <p>Here's the deletion:</p> <p>SceneManager contains an instance of DrawingScene, which inherits QGraphicsScene</p> <pre><code>void SceneManager::DeleteLink(int linkID, int startNodeID, int endNodeID) { // sceneData_ wraps the QMap&lt;int, QGraphicsItem&gt;. // This calls SceneTrackerData::deleteLink(int) (below) QGraphicsLineItem * link = sceneData_-&gt;deleteLink(linkID); qDebug() &lt;&lt; "Link to remove " &lt;&lt; link; // scene_ is an instance of DrawingScene, which inherits QGraphicsItem, so // this is just a vanila call to QGraphicsScene::removeItem() scene_-&gt;removeItem(link); // release the link from memory delete link; emit informLinkDeleted(startNodeID, endNodeID); } QGraphicsLineItem* SceneTrackerData::deleteLink(int linkid) { qDebug() &lt;&lt; "deleting link of id " &lt;&lt; linkid; QGraphicsLineItem * line = links_.take(linkid); qDebug() &lt;&lt; links_; return line; } </code></pre> <p>The warning is </p> <blockquote> <p>QGraphicsScene::removeItem: item 0x32fa7d8's scene (0x0) is different from this scene (0x1aabb60)</p> </blockquote> <p><strong>More edits</strong></p> <p>After adding some lines and ellipse items, this is how the scene->items() looks like after adding the objects. They all have parent = 0x0</p> <pre><code>(QGraphicsItem(this =0x3b980a8, parent =0x0, pos =QPointF(0, 0), z =-1, flags = {"isVisible|isEnabled" }), QGraphicsItem(this =0x3c32980, parent =0x0, pos =QPointF(326, 303), z =100, flags = {"isVisible|isEnabled" }), QGraphicsItem(this =0x3b9a1e0, parent =0x0, pos =QPointF(420, 410), z =100, flags = {"isVisible|isEnabled" }), QGraphicsItem(this =0x3f0ffc0, parent =0x0, pos =QPointF(411, 395), z =100, flags = {"isVisible|isEnabled" }), QGraphicsItem(this =0x3cf4600, parent =0x0, pos =QPointF(327, 302), z =0, flags = {"isVisible|isEnabled" })) </code></pre> <p>What I am trying to delete is QGraphicsItem(this =0x3cf4600), and here's the outcome:</p> <pre><code>Link to remove QGraphicsItem(this = 0x3cf4600 , parent = 0x0 , pos = QPointF(327, 302), z =0, flags = {"isVisible|isEnabled" }) Before removing: QGraphicsScene::removeItem: item 0x3cf4600's scene (0x0) is different from this scene (0x1e2ffb8) Scene after moving (QGraphicsItem(this =0x3b980a8, parent =0x0, pos =QPointF(0, 0), z =-1, flags = {"isVisible|isEnabled" }), QGraphicsItem(this =0x3c32980, parent =0x0, pos =QPointF(326, 303), z =100, flags = {"isVisible|isEnabled" }), QGraphicsItem(this =0x3b9a1e0, parent =0x0, pos =QPointF(420, 410), z =100, flags = {"isVisible|isEnabled" }), QGraphicsItem(this =0x3f0ffc0, parent =0x0, pos =QPointF(411, 395), z =100, flags = {"isVisible|isEnabled" })) </code></pre> <p>The behaviour is correct. I am puzzled why the warning is evening triggered at all.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload