Creating a new widget, very heavy paintEvent

Heads up! You've already completed this tutorial.

pmofidi7469 | 2020-10-13 15:40:57 UTC | #1

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.


martin | 2020-11-19 11:21:00 UTC | #2

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).

Create GUI Applications with Python & Qt5 by Martin Fitzpatrick — (PyQt5 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book

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!


pmofidi7469 | 2020-10-30 04:14:04 UTC | #3

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


The complete guide to packaging Python GUI applications with PyInstaller.
[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Creating a new widget, very heavy paintEvent was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.