23

I've been having this issue a lot of times.

When I modify some properties of a QWidget after the widget.show(), the widget won't update. Most of the time, a mouse click or when the mouse leaves or enters the widget, the widget will be updated. However, if I leave the mouse, it won't refresh by itself.

Until now I managed to deal with this by doing :

widget.hide()
widget.show()

But this is a very dirty fix. Is there a better way to tell python to refresh the widget ?

Thank you.

Jablonski
  • 18,083
  • 2
  • 46
  • 47
Olivier Giniaux
  • 870
  • 2
  • 8
  • 22
  • I tried update() on a parent of the widget but it wasn't doing anything. In fact, I juste realized that update() only updates the widget but not its children. Doing and update() on this widget worked! – Olivier Giniaux Jun 09 '15 at 10:51
  • So if your problem is solved, mark please one answer (the best for you) as accepted (to mark this question as solved too) – Jablonski Jun 09 '15 at 10:58

2 Answers2

36

To update the widget, you should repaint() it, but calling repaint() directly is not very good, so try:

widget.update()

From doc:

This function does not cause an immediate repaint; instead it schedules a paint event for processing when Qt returns to the main event loop. This permits Qt to optimize for more speed and less flicker than a call to repaint() does.

Calling update() several times normally results in just one paintEvent() call.

Qt normally erases the widget's area before the paintEvent() call. If the Qt::WA_OpaquePaintEvent widget attribute is set, the widget is responsible for painting all its pixels with an opaque color.

Jablonski
  • 18,083
  • 2
  • 46
  • 47
6

Did you already try the QWidget.update()

This function updates only the visible parts keeping the invisible parts untouched.

Bharadwaj
  • 737
  • 6
  • 26