How to animate the graph that I drew in PyQt?

So, I managed to get a graph drawn up on my screen, for example:

class Window(QWidget): #stuff graphicsView = QGraphicsView(self) scene = QGraphicsScene(self) #draw our nodes and edges. for i in range(0, len(MAIN_WORLD.currentMax.tour) - 1): node = QGraphicsRectItem(MAIN_WORLD.currentMax.tour[i][0]/3, MAIN_WORLD.currentMax.tour[i][1]/3, 5, 5) edge = QGraphicsLineItem(MAIN_WORLD.currentMax.tour[i][0]/3, MAIN_WORLD.currentMax.tour[i][1]/3, MAIN_WORLD.currentMax.tour[i+1][0]/3, MAIN_WORLD.currentMax.tour[i+1][1]/3) scene.addItem(node) scene.addItem(edge) #now go back and draw our connecting edge. Connects end to home node. connectingEdge = QGraphicsLineItem(MAIN_WORLD.currentMax.tour[0][0]/3, MAIN_WORLD.currentMax.tour[0][1]/3, MAIN_WORLD.currentMax.tour[len(MAIN_WORLD.currentMax.tour) - 1][0]/3, MAIN_WORLD.currentMax.tour[len(MAIN_WORLD.currentMax.tour) - 1][1]/3) scene.addItem(connectingEdge) graphicsView.setScene(scene) hbox = QVBoxLayout(self) #some more stuff.. hbox.addWidget(graphicsView) self.setLayout(hbox) 

Now the edges will be constantly updated, so I want to remove these edges and redraw them. How can i do this?

+4
source share
1 answer

QGraphicsScene controls the drawing of elements that you have added to it. If the position of the rectangles or lines has changed, you can update them if you are old on them:

 for i in range( ): nodes[i] = node = QGraphicsRectItem() scene.add(nodes[i]) 

Later you can update the node position:

 nodes[j].setRect(newx, newy, newwidth, newheight) 

Similarly for strings.

If you need to remove it, you can use

 scene.removeItem(nodes[22]) 
+2
source

Source: https://habr.com/ru/post/1300147/


All Articles