gpt4 book ai didi

c++ - QT 4.5 - QGraphicsItem 分配给 0x0 的父场景

转载 作者:行者123 更新时间:2023-11-30 03:12:11 24 4
gpt4 key购买 nike

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

class DrawingScene : public QGraphicsScene
{
...
}

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

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 以实现快速映射目的并一次性禁用其中的一组。所以这就是将项目添加到场景(如上)和 map 的方式

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: item 0x32fa7d8's scene (0x0) is different from this scene (0x1aabb60)

更多编辑

添加一些直线和椭圆项目后,这就是添加对象后 scene->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" }))

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

最佳答案

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

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

关于c++ - QT 4.5 - QGraphicsItem 分配给 0x0 的父场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1398461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com