QGraphicsScene dynamically adding shapes in specific coordinates
-
I want to draw a network graph in QT using ellipsis as nodes and lines as edges. The amount of nodes that while be placed as well as the spot that they will be placed on is user defined. For example: The user gives an vector with the elements 1,4,3,5 this means that there are going to be 4 columns of nodes and the first column will have 1 node, the second 4 nodes, e.t.c...
The problem with QGraphicsScene is that every time I add a node it changes the whole arrangement of the nodes and the coordinate system and I end up seeing only the nodes that should be placed in the 4rth quadrant of the plane, these nodes are in the top left corner though and nothing else is displayed.
There is also a difference in the colour of the nodes depending on their position but this doesn't matter. (Just making it clear because it affects the code.)
The way I draw them is shown below:
void drawNetworkGraphics(std::vector<int> topology){ QBrush blackBrush(Qt::black); QBrush blueBrush(Qt::blue); QPen blackPen(Qt::black); blackPen.setWidth(2); int middleH = graphicScene->width()/2; int middleV = graphicScene->height()/2; for(unsigned i = 0; i< topology.size(); i++){ if(i < topology.size()-1){ if(i < topology.size()/2){ for(unsigned k = 0; k<=topology[i]; k++){ if(k < topology[i]/2){ graphicScene->addEllipse(-(40*((topology.size()/2) - i)),-30*((topology[i]/2) -k),20,20,blackPen,blackBrush); } else{ if(k == topology[i]){ graphicScene->addEllipse(-40*((topology.size()/2) - i),(30*k)+1,20,20,blackPen,blueBrush); } else graphicScene->addEllipse(-40*((topology.size()/2) - i),(30*k)+1,20,20,blackPen,blackBrush); } } } else{ for(unsigned k = 0; k<=topology[i]; k++){ if(k < topology[i]/2){ graphicScene->addEllipse((40*i),-30*((topology[i]/2) -k),20,20,blackPen,blackBrush); } else{ if(k == topology[i]){ graphicScene->addEllipse((40*i)+1,(30*k),20,20,blackPen,blueBrush); } else graphicScene->addEllipse((40*i)+1,(30*k),20,20,blackPen,blackBrush); } } } } else{ for(unsigned k = 0; k<topology[i]; k++){ if(k < topology[i]/2){ graphicScene->addEllipse(40*i+1,-30*(topology[i]/2 -k),20,20,blackPen,blackBrush); } else{ graphicScene->addEllipse(40*i+1,30*k+1,20,20,blackPen,blackBrush); } } } } }```