【问题标题】:QT 4.5 - QGraphicsItem is assigned to parent scene of 0x0QT 4.5 - QGraphicsItem 分配给 0x0 的父场景
【发布时间】:2009-09-09 09:05:53
【问题描述】:

这是一个相当奇怪的情况。我有一个派生自 QGraphicsScene 的类

class DrawingScene : public QGraphicsScene
{
   ...
}

每当我在派生类中添加任何项目时,该项目最终都会附加到 0x0 的父级。问题是尽管如此,在场景中正常出现的图形项目,并且可以与之交互。比如这个sn-p的代码:

QGraphicsEllipseItem * newEllipse = addEllipse(rect, pen);
qDebug() << "Scene info " << this
newEllipse->setPos(pos);
newEllipse->setZValue(100);
qDebug() << "New point added = " << newEllipse;

这里有一些调试输出

Scene info  DrawingScene(0x1d2ffb8) 
New point added =  QGraphicsItem(this = 0x3f0fda0 , parent = 0x0 , pos = QPointF(326, 307), z =100, flags = {"isVisible|isEnabled" })   

每当我尝试删除该项目时,我都会收到警告说场景不同,但该项目仍然消失了。

我是否遗漏了什么,或者 QGraphicsScene 不是一开始就派生出来的?

编辑:删除代码

QGraphicsItems 被添加到 QMap 以实现快速映射目的并一次性禁用其中的一组。这就是将项目添加到场景(如上)和地图的方式

int SceneTrackerData::addLink(QGraphicsLineItem * newLink)
{
    links_.insert(currentLinkID_, newLink);

    qDebug() << links_;

    // Store the ID of the newly added link and increment link count
    int thisLinkID = currentLinkID_;    
    currentLinkID_++;

    newLink->setData(LinkData::LinkID, QVariant(thisLinkID));


    qDebug() << "Link " << thisLinkID << " connecting " << newLink->data(0).toInt()
             << " and " << newLink->data(1).toInt() << " added.";


    return thisLinkID;
}

这里是删除:

SceneManager 包含一个 DrawingScene 的实例,它继承了 QGraphicsScene

void SceneManager::DeleteLink(int linkID, int startNodeID, int endNodeID)
{
    // sceneData_ wraps the QMap<int, QGraphicsItem>.
    // This calls SceneTrackerData::deleteLink(int) (below)
    QGraphicsLineItem * link = sceneData_->deleteLink(linkID);

qDebug() << "Link to remove " << link;

    // scene_ is an instance of DrawingScene, which inherits QGraphicsItem, so
    // this is just a vanila call to QGraphicsScene::removeItem()
    scene_->removeItem(link);

    // release the link from memory
    delete link;

    emit informLinkDeleted(startNodeID, endNodeID);
}

QGraphicsLineItem* SceneTrackerData::deleteLink(int linkid)
{
    qDebug() << "deleting link of id " << linkid;
    QGraphicsLineItem * line = links_.take(linkid);
    qDebug() << links_;
    return line;

}

警告是

QGraphicsScene::removeItem: 项目 0x32fa7d8 的场景(0x0)不同 从这个场景(0x1aabb60)

更多修改

添加一些线条和椭圆项目后,这是添加对象后场景->items() 的样子。他们都有 parent = 0x0

(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" })) 

我要删除的是 QGraphicsItem(this =0x3cf4600),结果如下:

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" })) 

行为是正确的。我很困惑为什么晚上会触发警告。

【问题讨论】:

  • 导出QGraphicsScene的原因是什么?
  • 拦截mouseclickat、mousepressed等事件
  • @Extrakun 你能分享你的鼠标事件代码吗?我也在努力解决类似的问题。
  • 对你来说这是一个警告,实际上是The program has unexpectedly finished.

标签: c++ qt qt4


【解决方案1】:

这段代码没有任何问题。该项目未附加到 0x0 的父项。该项目附加到场景,这意味着它没有另一个项目作为其父项。这导致项目具有 NULL 作为其父项。你看到的0x0不是大小,是NULL指针。

我推荐阅读The Graphics View Framework。这是一个很好的起点,也是遇到错误时参考的好地方。

【讨论】:

  • 意思是Scene指向parent,而不是item指向parent
【解决方案2】:

我相信调试输出中的“parent = 0x0”是指父图形项(例如,与父 QObject 相对)。正如QGraphicsitem documentation 中提到的,图形项可以有父项(这意味着应用于父项的变换也将应用于子项)。

调用addEllipse(...) 将创建一个独立的图形项,没有父项(但会将其添加到场景中)。

关于你的删除代码, QGraphicsItem::scene() 返回什么

  1. 使用 addEllipse() 创建图形项后立即
  2. 就在移除图形项之前?

如果它在创建后立即返回 NULL,那么在创建图形项时出现了问题。如果时间较晚,则说明您的地图存储和检索代码中存在问题。

我很困惑,为什么您要存储指向QGraphicsLineItems 的指针,并创建QGraphicsEllipseItems?我认为这是一个错字?

【讨论】:

  • 是的,错字,但两个对象都会产生相同的问题,所以我在发布代码时感到困惑。
  • 嗨,我已经添加了场景的输出转储。该项目已正确删除,但仍会引发警告。
【解决方案3】:

因为此页面是“QGraphicsScene::removeItem: item 0xx 的场景 (0x0) 与此场景 (0xx) 不同”的热门搜索结果之一,我想发布此错误的另一个原因:场景已已清除,但项目仍然存在并从列表中指向。

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2015-04-17
    • 1970-01-01
    相关资源
    最近更新 更多