Gbamis Tobi Gbamis Tobi - 27 days ago 9
C++ Question

c++ program crashes when adding new QGraphicsItem to scene

My c++ program crashes when i add new QGraphicsLineItem to a QList of QGraphicsPixmapItem.
Below is the function.

void MainWindow::linkComputerNodes(QList<Node*> routers)
{

for(int i=0;i<routers.length();i++)
{
scene->addItem(new Link(routers.at(i),routers.at(i+1)));
}
}

Answer

Your for loop:

for(int i=0;i<routers.length();i++)

Goes through every single router in your list, but then you add a link from each router to the router directly after it. This means that for the last router, you are trying to add a link to one that doesn't exist. Try changing your loop to:

for(int i=0;i<routers.length() - 1;i++)

So that you only add links between existing routers.

Comments