C++ qt将构造函数中具有新qGraphicsitem的qGraphicsitem添加到QGraphicscene

C++ qt将构造函数中具有新qGraphicsitem的qGraphicsitem添加到QGraphicscene,c++,qt,qgraphicsscene,qgraphicsitem,C++,Qt,Qgraphicsscene,Qgraphicsitem,我在qGraphicsView的场景中添加了自定义QGraphicsitem(CoauthorGraphItem)。 并在QGraphicsItem的构造函数中创建新的QGraphicsItem(NodeItem,EdgeItem)。 然后在场景中,查看qGraphicsItem(CoauthorGraphItem)以及构造函数生成的qGraphicsItem(NodeItem、EdgeItem)。 我想知道为什么会这样。我找不到这份文件。帮帮我 这是我的密码 // CoauthorGraph

我在qGraphicsView的场景中添加了自定义QGraphicsitem(CoauthorGraphItem)。 并在QGraphicsItem的构造函数中创建新的QGraphicsItem(NodeItem,EdgeItem)。 然后在场景中,查看qGraphicsItem(CoauthorGraphItem)以及构造函数生成的qGraphicsItem(NodeItem、EdgeItem)。 我想知道为什么会这样。我找不到这份文件。帮帮我

这是我的密码

 // CoauthorGraphItem is inherit QGraphicsItem
    CoauthorGraphItem::CoauthorGraphItem(ifstream& fin)
    {
        ... // read file and create graph data

        for (boost::tie(ei, ei_end) = boost::edges(*graph); ei != ei_end; ++ei) {     
            u = source(*ei, *graph);
            v = target(*ei, *graph);
            Point p1 = position[u];
            Point p2 = position[v];

            //make edge item and it's also inherit QGraphicsItem
            EdgeItem *edge;
            edge = new EdgeItem(p1[0], p1[1], p2[0], p2[1], QColor(Qt::black), 0, index[ei->m_source], index[ei->m_target]);

            edge->setPos(p1[0], p1[1]);
            edgeList << edge;
        }
        //add nodes that's graph nodes
        for (boost::tie(vi, vi_end) = vertices(*graph); vi != vi_end; ++vi) {
            Point p = position[*vi];
            std::string name = label[*vi];

            //make nodeItem and it's also inherit QGraphicsItem
            NodeItem *node;
            node = new NodeItem(p[0], p[1], QColor(Qt::green), QString(name.c_str()), vInfo[i], vcoauthor[i]);
            node->setPos(QPointF(p[0], p[1]));
            nodeList << node;
            i++;
        }
    }
//CoauthorGraphItem是inherit QGraphicsItem
CoauthorGraphItem::CoauthorGraphItem(ifstream和fin)
{
…//读取文件并创建图形数据
对于(boost::tie(ei,ei_end)=boost::edges(*图);ei!=ei_end;++ei){
u=来源(*ei,*图表);
v=目标(*ei,*图);
点p1=位置[u];
点p2=位置[v];
//创建边缘项,它也将继承QGraphicsItem
边缘项*边缘;
边=新边项(p1[0],p1[1],p2[0],p2[1],QColor(Qt::黑色),0,索引[ei->m_源],索引[ei->m_目标];
边缘->设置位置(p1[0],p1[1]);
边缘组(QPointF(p[0],p[1]);

nodeList你能澄清一下吗?你有一个定制的QGraphicsitem(一个叫做nodelitem的?)您将其添加到场景中。在此自定义QGraphicsitem的构造函数中,您创建了另一个QGraphicsitem。现在您在场景中看到了两个项目?或者您只看到了第一个项目,但想同时看到这两个项目?不确定我是否了解您的实际问题。您可以添加构造函数的代码吗?抱歉,我修复了代码。我将其添加到场景合著者绘图中hItem是图形数据,然后我看到了CoauthorGraphItem、NodeItem和EdgeItem。我使用boost graph。好吧,这稍微澄清了一点,但有人认为我仍然不明白,你们说:“然后在场景中,查看构造函数(NodeItem、EdgeItem)生成的qGraphicsItem(CoauthorGraphItem)和qGraphicsItem。我想知道为什么会发生这种情况。”。问题是您在场景/视图中看不到NodeItem和EdgeItem,还是您可以看到它们但不希望看到它们?如果需要添加它们的方法,可以使用
qgraphicscene*QGraphicsItem::Scene()const
获取指向添加了合作作者项目的场景的指针,然后调用
void qgraphicscene::addItem(QM*项目)
并将其添加到场景中我可以看到它们,但我不想让它们被看到。我该怎么办?现在我知道你的问题是什么了。有趣的问题,我不确定是否有什么地方涵盖了为什么会发生这种情况,因为在另一个QGraphicsItem中创建QGraphicsItem是不寻常的,我从未见过。如果你不想让它们成为场景的一部分,为什么他们要继承QGraphicsItem?你能做一个表示NodeItem和EdgeItem的结构吗?