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

标签 c++ qt qt4

这是一个相当奇怪的情况。我有一个派生自 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/

相关文章:

windows - 如何在 Windows 上使用 Qt 获取开放端口的进程名称和二进制路径?

qt - QString 拆分

c++ - 使用 Qt 过滤 QFileInfoList 文件

qt - 在 Qt 中,如何对齐不同组框中的表单元素?

c++ - SFML RenderWindow 奇数字符

c++ - 访问类的私有(private)成员函数

c++ - 计算变化数组中的预期反转次数

c++ - 具有模板化构造函数(其他类型)的模板化类

c++ - 如何使用 qregexp 验证电子邮件地址

qt - 静态编译 QWebKit 4.6.2