views:

215

answers:

1

So I've managed to get a graph drawn up on my screen like such:

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 are going to be updating constantly, so I want to be able to remove those edges and redraw them. How can I do that?

+1  A: 

QGraphicsScene manages the drawing of the items you've added to it. If the position of the rectangles or lines has changed you can update them if you old onto them:

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

Later, you can update a node's position:

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

Similarly for lines.

If you need to remove one, you can use

scene.removeItem(nodes[22])
Jamey Hicks