Reputation: 73
Is there any way to get the pixels that will be displayed on a QWidget, do some processing, and then display the processed pixels?
I can't seem to overcome the limitations of paintEvent(), hopefully someone can help.
QPixmap::grabWidget and QWidget::render will get me the pixels I need, but they cannot be called from within paintEvent(), since doing so will trigger an infinite loop.
I have tried running a timer, taking a snapshot, doing my processing, forcing a repaint, and displaying the saved image. This works to some extent, but on dynamic content (i.e. moving) it fails miserably.
I need to be able to do this from within paintEvent().
Is there any way to do this?
Upvotes: 1
Views: 2281
Reputation: 7777
It sounds like your problem would be best solved by rendering the widget to a pixmap (within the paint event), doing your processing on the pixmap, then rendering the result to the widget afterwards:
void MySuperAwesomeWidget::paintEvent(QPaintEvent* event)
{
QPixmap pixmap(size());
QPainter painter;
painter.begin(&pixmap);
// Drawing code goes here
painter.end();
// Do processing on pixmap here
painter.begin(this);
painter.drawPixmap(0, 0, pixmap);
painter.end();
}
Normally, the technique I've described would be considered unnecessary (or even undesirable) because it is essentially a form of double-buffering and QWidget
already provides double-buffering behind the scenes. However, in your case you are doing some processing on the drawing before performing a final rendering. As such, this is probably the best approach.
Upvotes: 2