Creating a new widget, very heavy paintEvent

Im trying to develop a program which have many of my custom widget. In my widget i have a crosshair mouse pointer and when i move it in one of my widgets all of them must sense it and the crosshair must react on every single widget.
I have a self.update() on the end of the paintEnent function. every thing seems good but a very huge CPU usage.
as you said ’ If a widget is simple enough (like ours is) you can often get away with simply redrawing the entire thing any time anything happens. But for more complicated widgets this can get very inefficient. For these cases the paintEvent includes the specific region that needs to be updated. We’ll make use of this in later, more complicated examples.’

I have tried to do this but not successful. please make a good example.

Hey @pmofidi7469 welcome to the forum!

I have a self.update() on the end of the paintEnent function. every thing seems good but a very huge CPU usage.

If this is not a typo (mouseMoveEvent maybe?) then this could be the source of the CPU usage. A paintEvent is fired in response to .update() being called, so calling in in your paintEvent creates an effective infinite loop (though not quite, as updates can be ignored).

The performance benefit of clipping with QPaintEvent.region() is only in the drawing – are you redrawing all the content of your other widgets? If you just need to move a cursor around on an otherwise unchanged image, you can cache the base image and then just redraw the cursor on top, for example.

If this doesn’t make sense, can you post some code & I’ll take a look!

1 Like

I removed Update command from PaintEvent and i have used my custon Event to prevent updating all of region.
Thnanks :slight_smile:

1 Like