4

My paintEvent has access to a pointer whose value changes from time to time and what gets painted depends on these values. With basic debugging I'm sure that this function is being called but the window does not get updated. The new stuff only appears on the window when it loses focus to another application.

If this is about performance I can set a static variable in the paintEvent to check if the pointer has been updated or not to avoid unnecessary repaints. It would be nice if Qt would just paint when I told it to.

I was hoping someone could help me out or point me in the direction of the right documentation. Thanks.

NG_
  • 6,895
  • 7
  • 45
  • 67
user1872099
  • 95
  • 2
  • 5
  • 1
    How are you instructing it to repaint, are you calling `repaint()` or `update()`? – cmannett85 Jan 26 '13 at 10:28
  • I noticed that (latest?) QtCreator also fails to repaint source windows sometime. Could be a bug. I'm using Qt 4.8.1 on Ubuntu. – CapelliC Jan 26 '13 at 10:28
  • 1
    "set a static variable in the paintEvent to check if the pointer has been updated or not to avoid unnecessary repaints." - That won't work: paintEvent() must _always_ paint, at least the rect()/region() specified in the event. Whatever was painted before will be gone, so if you don't paint, you end up with an empty widget after the paint event. To cache something previously painted, one must do that using QPixmap, manually. – Frank Osterfeld Jan 26 '13 at 11:10
  • @cmannett I'm just using the overridden paintEvent() method inherited from QWidget. I havn't tried calling repaint() or update() from within here because it seems like it would cause an infinite loop. =3 CapelliC I think it's more likely we're doing somethign wrong than a bug inside the QT framewrok =D Frank Derp. Not sure why I thought that would work. – user1872099 Jan 26 '13 at 13:38
  • @user1872099 That's right, but not what I meant. In order for `paintEvent(..)` to be called, something has to tell the widget to repaint - I'm asking what method have you called in order to do that. – cmannett85 Jan 26 '13 at 13:59
  • @cmannett Thanks a lot for taking the time you answer. You've helped me figure out what the problem was. ^_^ – user1872099 Jan 27 '13 at 15:22

1 Answers1

4

you must call update() method to repaint your widget.

Reza Ebrahimi
  • 3,623
  • 2
  • 27
  • 41
  • Yeah, the issue was that I just included the code in the paintEvent() function and then mouseovering push buttons and stuff, seeing that the function was triggered but obviously it wasn't repainting the whole window, just the parts that change i.e. the button or whatever. Then switching windows caused a full repaint. I'm sorry for the silly quest. Thanks for the help. – user1872099 Jan 27 '13 at 15:24