views:

372

answers:

1

I have a QScrollArea fathering my awesome scrolling widget. I like to do updates on the contents on various occasions.

For this I did an override of paintEvent(QPaintEvent *). Then everytime I want it to be done I call update() on the widget.

Problem: paintEvent() is never called by this!

What I tried in troubleshooting so far:

  • Use repaint() instead of update(). Should call it instantanously. Unfortunately does not.
  • Test for isVisible() and updatesEnabled
  • Test wether my override is correct. Resizing the window calls my function. Only my manual update(), repaint() calls fail.
  • Implement a QTimer to trigger update() or repaint() every 500ms. The trigger gives text output, the function is not called.

Anybody got an idea what to check next? What could make repaint() not call paintEvent()?

+1  A: 

I am going to post an answer even though its a old post as the info may be useful to people like me who faced the same problem. The solution is to call this->viewport()->repaint() or this->viewport()->update() from your QAbstractScrollArea derived class instead of just repaint() or update().

More Info is given in Qt documentation -

*QWidget * QAbstractScrollArea::viewport () const Returns the viewport widget. Use the QScrollArea::widget() function to retrieve the contents of the viewport widget.

Since the contents that we have in our QAbstractScrollArea derived class will be displayed in the viewport widget, makes sense to call viewport widget's update or repaint to draw our data again (have our paintevent called).

Abhiram
Basically this is the correct answer. My fault was that I created my custom widget from QScrollArea and thought I could do my individual drawing there. Instead, I would have to override the viewport's drawing function, probably not possible.
ypnos
It is possible to override viewport's drawing function. Only thing you need to take care of inside viewport's reimplemented paintEvent() is- Instead of passing "this" pointer as one would normally do while creating a QPainter object, pass this->viewport(). QPainter(this->viewport()) instead of QPainter(this). Otherwise "QPainter is not active" error is thrown.
Abhiram