Skip to content
Advertisement

How to move rect in pyqtgraph

I try to have a rect which moves with the mouse pointer. However, I seem to have a problem with calling the paint method. It either doesn’t draw or creates artifacts. The only clean picture I get is with a forced redraw (zoom the graph).

Here’s my mwe

JavaScript

Advertisement

Answer

Just redrawing the contents of an internal QPicture doesn’t affect the graphics item in any way, you must call update() in order to correctly refresh the item. Also consider that since you’re actually changing the bounding rect of the item, you must also call prepareGeometryChange() before changing the geometry, otherwise you’ll see artifacts due to the previous rectangle position not correctly cleared up.

JavaScript

Also, if you only need a rectangle to move around with the mouse, consider using the simpler QGraphicsRectItem which doesn’t require any painting implementation.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement