A Replaying QPainter
Mirko Boehm
mirko at kde.org
Wed Jul 4 21:51:10 BST 2007
Hi,
today in Simons "Qt tips and tricks" session we shortly discussed that it
would be interesting to follow a paint operation step by step on the display.
I have checked some kind of a "proof of concept" into
playground/devtools/QReplayingPainter. There is an example program that, when
ran, creates an example widget and a second one in which the different steps
of the painting operation can be reproduced.
The solution is not perfect. Especially, QPainter's interface is not virtual,
which means when the pointer to the replaying painter to another function,
the called function only sees the base class pointer, and the paint
operations are not replayed.
The example program shows that still this is already usable for some
scenarios:
void TestWidget::paintEvent( QPaintEvent* e )
{
Q_UNUSED( e ); // for now
#ifdef REPLAY_PAINTING
QReplayingPainter p( this );
#else
QPainter p( this );
#endif
p.setRenderHint( QPainter::Antialiasing, true );
p.setPen( QPen( Qt::red, 2 ) );
p.setBrush( Qt::white );
QRect rect( 10, 10, width() - 20, height() - 20 );
p.drawRect( rect );
p.setPen( QPen( Qt::blue, 2 ) );
p.setBrush( Qt::yellow );
QRect rect2( 20, 20, width() - 40, height() - 40 );
p.drawRect( rect2 );
p.setPen( QPen( QColor( "orange" ), 2 ) );
p.setBrush( Qt::red );
p.drawEllipse( rect2 );
int length = qMin( width(), height() ) / 3;
p.save();
p.translate( width() / 2, height() / 2 );
....
And since this is a debugging tool, there is always the trick of defining
QPainter to QReplayingPainter in the respective compilation unit. But we
would never do that, right :-)
Another solution would be to write a QPaintEngine decorator, and plug it into
the QPaintDevice. This has the disadvantage that one call to a QPainter paint
operation may result in multiple frames in the "replay movie". Which dfaure
seems to prefer, and I do not.
Anyway, I think some Oslo residents will take the idea home to add
functionality like this to QPainter. This would of course be totally kewl,
and because of that, I will stop right here.
The API of QReplayingPainter wraps the complete QPainter API as of 4.3, so it
is in fact quite usable and useful. Have fun with it.
Best regards,
--Mirko.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 186 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20070704/ac52864b/attachment.sig>
More information about the kde-core-devel
mailing list