diff --git a/src/gui/kernel/qevent.cpp b/src/gui/kernel/qevent.cpp
index b0f7adf..a96cbd2 100644
--- a/src/gui/kernel/qevent.cpp
+++ b/src/gui/kernel/qevent.cpp
@@ -3850,6 +3850,8 @@ QDebug operator<<(QDebug dbg, const QEvent *e)
             dbg << ", ";
             QtDebugUtils::formatQEnum(dbg, button);
         }
+        if (!me->spontaneous())
+            dbg << ", non-spontaneous";
         if (buttons && button != buttons) {
             dbg << ", buttons=";
             QtDebugUtils::formatQFlags(dbg, buttons);
@@ -3867,7 +3869,11 @@ QDebug operator<<(QDebug dbg, const QEvent *e)
 #  ifndef QT_NO_WHEELEVENT
     case QEvent::Wheel: {
         const QWheelEvent *we = static_cast<const QWheelEvent *>(e);
-        dbg << "QWheelEvent(" << "pixelDelta=" << we->pixelDelta() << ", angleDelta=" << we->angleDelta() << ')';
+        dbg << "QWheelEvent(" << "pixelDelta=" << we->pixelDelta()
+            << ", angleDelta=" << we->angleDelta();
+        if (!we->spontaneous())
+            dbg << ", non-spontaneous";
+         dbg << ')';
     }
         break;
 #  endif // !QT_NO_WHEELEVENT
@@ -3885,6 +3891,8 @@ QDebug operator<<(QDebug dbg, const QEvent *e)
             dbg << ", text=" << ke->text();
         if (ke->isAutoRepeat())
             dbg << ", autorepeat, count=" << ke->count();
+        if (!ke->spontaneous())
+            dbg << ", non-spontaneous";
         dbg << ')';
     }
         break;
@@ -3997,6 +4005,8 @@ QDebug operator<<(QDebug dbg, const QEvent *e)
     default:
         dbg << eventClassName(type) << '(';
         QtDebugUtils::formatQEnum(dbg, type);
+        if (!e->spontaneous())
+            dbg << ", non-spontaneous";
         dbg << ", " << (const void *)e << ')';
         break;
     }
diff --git a/src/gui/painting/qpaintengine_raster.cpp b/src/gui/painting/qpaintengine_raster.cpp
index 9d981dc..c54388e 100644
--- a/src/gui/painting/qpaintengine_raster.cpp
+++ b/src/gui/painting/qpaintengine_raster.cpp
@@ -331,15 +331,18 @@ QRasterPaintEngine::QRasterPaintEngine(QPaintDevice *device)
     : QPaintEngineEx(*(new QRasterPaintEnginePrivate))
 {
     d_func()->device = device;
+    qDebug() << __FUNCTION__ << this << "dev=" << device;
     init();
 }
 
+
 /*!
     \internal
 */
 QRasterPaintEngine::QRasterPaintEngine(QRasterPaintEnginePrivate &dd, QPaintDevice *device)
     : QPaintEngineEx(dd)
 {
+    qDebug() << __FUNCTION__ << this << "dev=" << device;
     d_func()->device = device;
     init();
 }
@@ -419,6 +422,7 @@ void QRasterPaintEngine::init()
 */
 QRasterPaintEngine::~QRasterPaintEngine()
 {
+    qDebug() << __FUNCTION__ << this;
     Q_D(QRasterPaintEngine);
 
     qt_ft_grays_raster.raster_done(*d->grayRaster.data());
@@ -430,6 +434,7 @@ QRasterPaintEngine::~QRasterPaintEngine()
 bool QRasterPaintEngine::begin(QPaintDevice *device)
 {
     Q_D(QRasterPaintEngine);
+    qDebug() << __FUNCTION__ << this << "dev=" << device;
 
     if (device->devType() == QInternal::Pixmap) {
         QPixmap *pixmap = static_cast<QPixmap *>(device);
@@ -505,6 +510,7 @@ bool QRasterPaintEngine::begin(QPaintDevice *device)
 */
 bool QRasterPaintEngine::end()
 {
+    qDebug() << __FUNCTION__ << this  ;
 #ifdef QT_DEBUG_DRAW
     Q_D(QRasterPaintEngine);
     qDebug() << "QRasterPaintEngine::end devRect:" << d->deviceRect;
@@ -659,6 +665,7 @@ QPainterState *QRasterPaintEngine::createState(QPainterState *orig) const
 */
 void QRasterPaintEngine::setState(QPainterState *s)
 {
+    qDebug() << __FUNCTION__ << this << s;
     Q_D(QRasterPaintEngine);
     QPaintEngineEx::setState(s);
     d->rasterBuffer->compositionMode = s->composition_mode;
@@ -683,6 +690,13 @@ void QRasterPaintEngine::penChanged()
     qDebug() << "QRasterPaintEngine::penChanged():" << state()->pen;
 #endif
     QRasterPaintEngineState *s = state();
+    qDebug() << __FUNCTION__ <<  this << s;
+    if (s) {
+        qDebug()  << __FUNCTION__ <<  this << "sta=" << s << "painter=" << s->painter;
+    } else {
+        qDebug("NULL!!!");
+    }
+    Q_ASSERT(s);
     s->strokeFlags |= DirtyPen;
     s->dirty |= DirtyPen;
 }
@@ -2194,6 +2208,7 @@ void QRasterPaintEngine::drawImage(const QRectF &r, const QImage &img, const QRe
 
     Q_D(QRasterPaintEngine);
     QRasterPaintEngineState *s = state();
+    Q_ASSERT(s);
     int sr_l = qFloor(sr.left());
     int sr_r = qCeil(sr.right()) - 1;
     int sr_t = qFloor(sr.top());
diff --git a/src/gui/painting/qpainter.cpp b/src/gui/painting/qpainter.cpp
index 5dcab11..4795e5c 100644
--- a/src/gui/painting/qpainter.cpp
+++ b/src/gui/painting/qpainter.cpp
@@ -74,7 +74,7 @@ QT_BEGIN_NAMESPACE
 #define QGradient_StretchToDevice 0x10000000
 #define QPaintEngine_OpaqueBackground 0x40000000
 
-// #define QT_DEBUG_DRAW
+#define QT_DEBUG_DRAW
 #ifdef QT_DEBUG_DRAW
 bool qt_show_painter_debug_output = true;
 #endif
@@ -240,6 +240,7 @@ QTransform QPainterPrivate::hidpiScaleTransform() const
 */
 bool QPainterPrivate::attachPainterPrivate(QPainter *q, QPaintDevice *pdev)
 {
+    qDebug() << __FUNCTION__ << q << pdev;
     Q_ASSERT(q);
     Q_ASSERT(pdev);
 
@@ -305,6 +306,7 @@ bool QPainterPrivate::attachPainterPrivate(QPainter *q, QPaintDevice *pdev)
 
 void QPainterPrivate::detachPainterPrivate(QPainter *q)
 {
+    qDebug() << __FUNCTION__ << q;
     Q_ASSERT(refcount > 1);
     Q_ASSERT(q);
 
@@ -334,7 +336,7 @@ void QPainterPrivate::draw_helper(const QPainterPath &originalPath, DrawOperatio
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output) {
-        printf("QPainter::drawHelper\n");
+        fprintf(stderr, "QPainter::drawHelper\n");
     }
 #endif
 
@@ -652,7 +654,7 @@ void QPainterPrivate::updateMatrix()
 
     state->matrix *= hidpiScaleTransform();
 
-//     printf("VxF=%d, WxF=%d\n", state->VxF, state->WxF);
+//     fprintf(stderr, "VxF=%d, WxF=%d\n", state->VxF, state->WxF);
 //     qDebug() << " --- using matrix" << state->matrix << redirection_offset;
 }
 
@@ -1436,6 +1438,7 @@ void QPainterPrivate::updateState(QPainterState *newState)
 QPainter::QPainter()
     : d_ptr(new QPainterPrivate(this))
 {
+    qDebug() << __FUNCTION__ << this;
 }
 
 /*!
@@ -1465,6 +1468,7 @@ QPainter::QPainter()
 QPainter::QPainter(QPaintDevice *pd)
     : d_ptr(0)
 {
+    qDebug() << __FUNCTION__ << this << "pd=" <<pd;
     Q_ASSERT(pd != 0);
     if (!QPainterPrivate::attachPainterPrivate(this, pd)) {
         d_ptr.reset(new QPainterPrivate(this));
@@ -1478,6 +1482,7 @@ QPainter::QPainter(QPaintDevice *pd)
 */
 QPainter::~QPainter()
 {
+    qDebug() << __FUNCTION__ << this;
     d_ptr->inDestructor = true;
     QT_TRY {
         if (isActive())
@@ -1566,7 +1571,7 @@ void QPainter::save()
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::save()\n");
+        fprintf(stderr, "QPainter::save()\n");
 #endif
     Q_D(QPainter);
     if (!d->engine) {
@@ -1596,7 +1601,7 @@ void QPainter::restore()
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::restore()\n");
+        fprintf(stderr, "QPainter::restore()\n");
 #endif
     Q_D(QPainter);
     if (d->states.size()<=1) {
@@ -1687,8 +1692,10 @@ void QPainter::restore()
 */
 
 static inline void qt_cleanup_painter_state(QPainterPrivate *d)
-{
+{    
     d->states.clear();
+    qDebug() << __FUNCTION__ << "deleting p=" << d->q_ptr << "pE=" << d->engine
+    << "state" << d->state;
     delete d->state;
     d->state = 0;
     d->engine = 0;
@@ -1697,6 +1704,7 @@ static inline void qt_cleanup_painter_state(QPainterPrivate *d)
 
 bool QPainter::begin(QPaintDevice *pd)
 {
+    qDebug() << __FUNCTION__ << this <<"pd=" << pd;
     Q_ASSERT(pd);
 
     if (pd->painters > 0) {
@@ -1724,7 +1732,7 @@ bool QPainter::begin(QPaintDevice *pd)
 
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::begin(), device=%p, type=%d\n", pd, pd->devType());
+        fprintf(stderr, "QPainter::begin(), device=%p, type=%d\n", pd, pd->devType());
 #endif
 
     if (pd->devType() == QInternal::Pixmap)
@@ -1767,6 +1775,7 @@ bool QPainter::begin(QPaintDevice *pd)
             Q_ASSERT(pm);
             if (pm->isNull()) {
                 qWarning("QPainter::begin: Cannot paint on a null pixmap");
+                qDebug() << __FUNCTION__ << "qt_cleanup_painter_state(d)";
                 qt_cleanup_painter_state(d);
                 return false;
             }
@@ -1783,10 +1792,12 @@ bool QPainter::begin(QPaintDevice *pd)
             Q_ASSERT(img);
             if (img->isNull()) {
                 qWarning("QPainter::begin: Cannot paint on a null image");
+                qDebug() << __FUNCTION__ << "qt_cleanup_painter_state(d)";
                 qt_cleanup_painter_state(d);
                 return false;
             } else if (img->format() == QImage::Format_Indexed8) {
                 // Painting on indexed8 images is not supported.
+                qDebug() << __FUNCTION__ << "qt_cleanup_painter_state(d)";
                 qWarning("QPainter::begin: Cannot paint on an image with the QImage::Format_Indexed8 format");
                 qt_cleanup_painter_state(d);
                 return false;
@@ -1811,6 +1822,7 @@ bool QPainter::begin(QPaintDevice *pd)
         if (d->engine->isActive()) {
             end();
         } else {
+            qDebug() << __FUNCTION__ << "qt_cleanup_painter_state(d)";
             qt_cleanup_painter_state(d);
         }
         return false;
@@ -1866,14 +1878,16 @@ bool QPainter::begin(QPaintDevice *pd)
 
 bool QPainter::end()
 {
+    qDebug() << __FUNCTION__ << this;
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::end()\n");
+        fprintf(stderr, "QPainter::end()\n");
 #endif
     Q_D(QPainter);
 
     if (!d->engine) {
         qWarning("QPainter::end: Painter not active, aborted");
+        qDebug() << __FUNCTION__ << "qt_cleanup_painter_state(d)";
         qt_cleanup_painter_state(d);
         return false;
     }
@@ -1913,6 +1927,7 @@ bool QPainter::end()
     if (d->extended) {
         d->extended = 0;
     }
+    qDebug() << __FUNCTION__ << "qt_cleanup_painter_state(d)";
 
     qt_cleanup_painter_state(d);
 
@@ -2124,7 +2139,7 @@ void QPainter::setBrushOrigin(const QPointF &p)
     Q_D(QPainter);
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::setBrushOrigin(), (%.2f,%.2f)\n", p.x(), p.y());
+        fprintf(stderr, "QPainter::setBrushOrigin(), (%.2f,%.2f)\n", p.x(), p.y());
 #endif
 
     if (!d->engine) {
@@ -2451,7 +2466,7 @@ void QPainter::setClipping(bool enable)
     Q_D(QPainter);
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::setClipping(), enable=%s, was=%s\n",
+        fprintf(stderr, "QPainter::setClipping(), enable=%s, was=%s\n",
                enable ? "on" : "off",
                hasClipping() ? "on" : "off");
 #endif
@@ -2830,7 +2845,7 @@ void QPainter::setClipRegion(const QRegion &r, Qt::ClipOperation op)
 #ifdef QT_DEBUG_DRAW
     QRect rect = r.boundingRect();
     if (qt_show_painter_debug_output)
-        printf("QPainter::setClipRegion(), size=%d, [%d,%d,%d,%d]\n",
+        fprintf(stderr, "QPainter::setClipRegion(), size=%d, [%d,%d,%d,%d]\n",
            r.rects().size(), rect.x(), rect.y(), rect.width(), rect.height());
 #endif
     if (!d->engine) {
@@ -3046,7 +3061,7 @@ void QPainter::setWorldMatrixEnabled(bool enable)
     Q_D(QPainter);
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::setMatrixEnabled(), enable=%d\n", enable);
+        fprintf(stderr, "QPainter::setMatrixEnabled(), enable=%d\n", enable);
 #endif
 
     if (!d->engine) {
@@ -3115,7 +3130,7 @@ void QPainter::scale(qreal sx, qreal sy)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::scale(), sx=%f, sy=%f\n", sx, sy);
+        fprintf(stderr, "QPainter::scale(), sx=%f, sy=%f\n", sx, sy);
 #endif
     Q_D(QPainter);
     if (!d->engine) {
@@ -3138,7 +3153,7 @@ void QPainter::shear(qreal sh, qreal sv)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::shear(), sh=%f, sv=%f\n", sh, sv);
+        fprintf(stderr, "QPainter::shear(), sh=%f, sv=%f\n", sh, sv);
 #endif
     Q_D(QPainter);
     if (!d->engine) {
@@ -3163,7 +3178,7 @@ void QPainter::rotate(qreal a)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::rotate(), angle=%f\n", a);
+        fprintf(stderr, "QPainter::rotate(), angle=%f\n", a);
 #endif
     Q_D(QPainter);
     if (!d->engine) {
@@ -3188,7 +3203,7 @@ void QPainter::translate(const QPointF &offset)
     qreal dy = offset.y();
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::translate(), dx=%f, dy=%f\n", dx, dy);
+        fprintf(stderr, "QPainter::translate(), dx=%f, dy=%f\n", dx, dy);
 #endif
     Q_D(QPainter);
     if (!d->engine) {
@@ -3232,7 +3247,7 @@ void QPainter::setClipPath(const QPainterPath &path, Qt::ClipOperation op)
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output) {
         QRectF b = path.boundingRect();
-        printf("QPainter::setClipPath(), size=%d, op=%d, bounds=[%.2f,%.2f,%.2f,%.2f]\n",
+        fprintf(stderr, "QPainter::setClipPath(), size=%d, op=%d, bounds=[%.2f,%.2f,%.2f,%.2f]\n",
                path.elementCount(), op, b.x(), b.y(), b.width(), b.height());
     }
 #endif
@@ -3370,7 +3385,7 @@ void QPainter::drawPath(const QPainterPath &path)
 #ifdef QT_DEBUG_DRAW
     QRectF pathBounds = path.boundingRect();
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawPath(), size=%d, [%.2f,%.2f,%.2f,%.2f]\n",
+        fprintf(stderr, "QPainter::drawPath(), size=%d, [%.2f,%.2f,%.2f,%.2f]\n",
                path.elementCount(),
                pathBounds.x(), pathBounds.y(), pathBounds.width(), pathBounds.height());
 #endif
@@ -3485,7 +3500,7 @@ void QPainter::drawRects(const QRectF *rects, int rectCount)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawRects(), count=%d\n", rectCount);
+        fprintf(stderr, "QPainter::drawRects(), count=%d\n", rectCount);
 #endif
     Q_D(QPainter);
 
@@ -3545,7 +3560,7 @@ void QPainter::drawRects(const QRect *rects, int rectCount)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawRects(), count=%d\n", rectCount);
+        fprintf(stderr, "QPainter::drawRects(), count=%d\n", rectCount);
 #endif
     Q_D(QPainter);
 
@@ -3645,7 +3660,7 @@ void QPainter::drawPoints(const QPointF *points, int pointCount)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawPoints(), count=%d\n", pointCount);
+        fprintf(stderr, "QPainter::drawPoints(), count=%d\n", pointCount);
 #endif
     Q_D(QPainter);
 
@@ -3707,7 +3722,7 @@ void QPainter::drawPoints(const QPoint *points, int pointCount)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawPoints(), count=%d\n", pointCount);
+        fprintf(stderr, "QPainter::drawPoints(), count=%d\n", pointCount);
 #endif
     Q_D(QPainter);
 
@@ -3792,7 +3807,7 @@ void QPainter::setBackgroundMode(Qt::BGMode mode)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::setBackgroundMode(), mode=%d\n", mode);
+        fprintf(stderr, "QPainter::setBackgroundMode(), mode=%d\n", mode);
 #endif
 
     Q_D(QPainter);
@@ -3838,7 +3853,7 @@ void QPainter::setPen(const QColor &color)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::setPen(), color=%04x\n", color.rgb());
+        fprintf(stderr, "QPainter::setPen(), color=%04x\n", color.rgb());
 #endif
     Q_D(QPainter);
     if (!d->engine) {
@@ -3872,7 +3887,7 @@ void QPainter::setPen(const QPen &pen)
 
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::setPen(), color=%04x, (brushStyle=%d) style=%d, cap=%d, join=%d\n",
+        fprintf(stderr, "QPainter::setPen(), color=%04x, (brushStyle=%d) style=%d, cap=%d, join=%d\n",
            pen.color().rgb(), pen.brush().style(), pen.style(), pen.capStyle(), pen.joinStyle());
 #endif
     Q_D(QPainter);
@@ -3953,7 +3968,7 @@ void QPainter::setBrush(const QBrush &brush)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::setBrush(), color=%04x, style=%d\n", brush.color().rgb(), brush.style());
+        fprintf(stderr, "QPainter::setBrush(), color=%04x, style=%d\n", brush.color().rgb(), brush.style());
 #endif
     Q_D(QPainter);
     if (!d->engine) {
@@ -4034,7 +4049,7 @@ void QPainter::setBackground(const QBrush &bg)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::setBackground(), color=%04x, style=%d\n", bg.color().rgb(), bg.style());
+        fprintf(stderr, "QPainter::setBackground(), color=%04x, style=%d\n", bg.color().rgb(), bg.style());
 #endif
 
     Q_D(QPainter);
@@ -4066,7 +4081,7 @@ void QPainter::setFont(const QFont &font)
 
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::setFont(), family=%s, pointSize=%d\n", font.family().toLatin1().constData(), font.pointSize());
+        fprintf(stderr, "QPainter::setFont(), family=%s, pointSize=%d\n", font.family().toLatin1().constData(), font.pointSize());
 #endif
 
     if (!d->engine) {
@@ -4122,7 +4137,7 @@ void QPainter::drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius,
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawRoundedRect(), [%.2f,%.2f,%.2f,%.2f]\n", rect.x(), rect.y(), rect.width(), rect.height());
+        fprintf(stderr, "QPainter::drawRoundedRect(), [%.2f,%.2f,%.2f,%.2f]\n", rect.x(), rect.y(), rect.width(), rect.height());
 #endif
     Q_D(QPainter);
 
@@ -4222,7 +4237,7 @@ void QPainter::drawEllipse(const QRectF &r)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawEllipse(), [%.2f,%.2f,%.2f,%.2f]\n", r.x(), r.y(), r.width(), r.height());
+        fprintf(stderr, "QPainter::drawEllipse(), [%.2f,%.2f,%.2f,%.2f]\n", r.x(), r.y(), r.width(), r.height());
 #endif
     Q_D(QPainter);
 
@@ -4263,7 +4278,7 @@ void QPainter::drawEllipse(const QRect &r)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawEllipse(), [%d,%d,%d,%d]\n", r.x(), r.y(), r.width(), r.height());
+        fprintf(stderr, "QPainter::drawEllipse(), [%d,%d,%d,%d]\n", r.x(), r.y(), r.width(), r.height());
 #endif
     Q_D(QPainter);
 
@@ -4349,7 +4364,7 @@ void QPainter::drawArc(const QRectF &r, int a, int alen)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawArc(), [%.2f,%.2f,%.2f,%.2f], angle=%d, sweep=%d\n",
+        fprintf(stderr, "QPainter::drawArc(), [%.2f,%.2f,%.2f,%.2f], angle=%d, sweep=%d\n",
            r.x(), r.y(), r.width(), r.height(), a/16, alen/16);
 #endif
     Q_D(QPainter);
@@ -4411,7 +4426,7 @@ void QPainter::drawPie(const QRectF &r, int a, int alen)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawPie(), [%.2f,%.2f,%.2f,%.2f], angle=%d, sweep=%d\n",
+        fprintf(stderr, "QPainter::drawPie(), [%.2f,%.2f,%.2f,%.2f], angle=%d, sweep=%d\n",
            r.x(), r.y(), r.width(), r.height(), a/16, alen/16);
 #endif
     Q_D(QPainter);
@@ -4480,7 +4495,7 @@ void QPainter::drawChord(const QRectF &r, int a, int alen)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawChord(), [%.2f,%.2f,%.2f,%.2f], angle=%d, sweep=%d\n",
+        fprintf(stderr, "QPainter::drawChord(), [%.2f,%.2f,%.2f,%.2f], angle=%d, sweep=%d\n",
            r.x(), r.y(), r.width(), r.height(), a/16, alen/16);
 #endif
     Q_D(QPainter);
@@ -4527,7 +4542,7 @@ void QPainter::drawLines(const QLineF *lines, int lineCount)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawLines(), line count=%d\n", lineCount);
+        fprintf(stderr, "QPainter::drawLines(), line count=%d\n", lineCount);
 #endif
 
     Q_D(QPainter);
@@ -4576,7 +4591,7 @@ void QPainter::drawLines(const QLine *lines, int lineCount)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawLine(), line count=%d\n", lineCount);
+        fprintf(stderr, "QPainter::drawLine(), line count=%d\n", lineCount);
 #endif
 
     Q_D(QPainter);
@@ -4695,7 +4710,7 @@ void QPainter::drawPolyline(const QPointF *points, int pointCount)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawPolyline(), count=%d\n", pointCount);
+        fprintf(stderr, "QPainter::drawPolyline(), count=%d\n", pointCount);
 #endif
     Q_D(QPainter);
 
@@ -4736,7 +4751,7 @@ void QPainter::drawPolyline(const QPoint *points, int pointCount)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawPolyline(), count=%d\n", pointCount);
+        fprintf(stderr, "QPainter::drawPolyline(), count=%d\n", pointCount);
 #endif
     Q_D(QPainter);
 
@@ -4811,7 +4826,7 @@ void QPainter::drawPolygon(const QPointF *points, int pointCount, Qt::FillRule f
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawPolygon(), count=%d\n", pointCount);
+        fprintf(stderr, "QPainter::drawPolygon(), count=%d\n", pointCount);
 #endif
 
     Q_D(QPainter);
@@ -4850,7 +4865,7 @@ void QPainter::drawPolygon(const QPoint *points, int pointCount, Qt::FillRule fi
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawPolygon(), count=%d\n", pointCount);
+        fprintf(stderr, "QPainter::drawPolygon(), count=%d\n", pointCount);
 #endif
 
     Q_D(QPainter);
@@ -4949,7 +4964,7 @@ void QPainter::drawConvexPolygon(const QPoint *points, int pointCount)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawConvexPolygon(), count=%d\n", pointCount);
+        fprintf(stderr, "QPainter::drawConvexPolygon(), count=%d\n", pointCount);
 #endif
 
     Q_D(QPainter);
@@ -4983,7 +4998,7 @@ void QPainter::drawConvexPolygon(const QPointF *points, int pointCount)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawConvexPolygon(), count=%d\n", pointCount);
+        fprintf(stderr, "QPainter::drawConvexPolygon(), count=%d\n", pointCount);
 #endif
 
     Q_D(QPainter);
@@ -5047,7 +5062,7 @@ void QPainter::drawPixmap(const QPointF &p, const QPixmap &pm)
 {
 #if defined QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawPixmap(), p=[%.2f,%.2f], pix=[%d,%d]\n",
+        fprintf(stderr, "QPainter::drawPixmap(), p=[%.2f,%.2f], pix=[%d,%d]\n",
                p.x(), p.y(),
                pm.width(), pm.height());
 #endif
@@ -5119,7 +5134,7 @@ void QPainter::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
 {
 #if defined QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawPixmap(), target=[%.2f,%.2f,%.2f,%.2f], pix=[%d,%d], source=[%.2f,%.2f,%.2f,%.2f]\n",
+        fprintf(stderr, "QPainter::drawPixmap(), target=[%.2f,%.2f,%.2f,%.2f], pix=[%d,%d], source=[%.2f,%.2f,%.2f,%.2f]\n",
                r.x(), r.y(), r.width(), r.height(),
                pm.width(), pm.height(),
                sr.x(), sr.y(), sr.width(), sr.height());
@@ -5839,7 +5854,7 @@ void QPainter::drawText(const QPointF &p, const QString &str, int tf, int justif
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawText(), pos=[%.2f,%.2f], str='%s'\n", p.x(), p.y(), str.toLatin1().constData());
+        fprintf(stderr, "QPainter::drawText(), pos=[%.2f,%.2f], str='%s'\n", p.x(), p.y(), str.toLatin1().constData());
 #endif
 
     Q_D(QPainter);
@@ -5918,7 +5933,7 @@ void QPainter::drawText(const QRect &r, int flags, const QString &str, QRect *br
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawText(), r=[%d,%d,%d,%d], flags=%d, str='%s'\n",
+        fprintf(stderr, "QPainter::drawText(), r=[%d,%d,%d,%d], flags=%d, str='%s'\n",
            r.x(), r.y(), r.width(), r.height(), flags, str.toLatin1().constData());
 #endif
 
@@ -6005,7 +6020,7 @@ void QPainter::drawText(const QRectF &r, int flags, const QString &str, QRectF *
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawText(), r=[%.2f,%.2f,%.2f,%.2f], flags=%d, str='%s'\n",
+        fprintf(stderr, "QPainter::drawText(), r=[%.2f,%.2f,%.2f,%.2f], flags=%d, str='%s'\n",
            r.x(), r.y(), r.width(), r.height(), flags, str.toLatin1().constData());
 #endif
 
@@ -6123,7 +6138,7 @@ void QPainter::drawText(const QRectF &r, const QString &text, const QTextOption
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawText(), r=[%.2f,%.2f,%.2f,%.2f], str='%s'\n",
+        fprintf(stderr, "QPainter::drawText(), r=[%.2f,%.2f,%.2f,%.2f], str='%s'\n",
            r.x(), r.y(), r.width(), r.height(), text.toLatin1().constData());
 #endif
 
@@ -6375,7 +6390,7 @@ void QPainterPrivate::drawTextItem(const QPointF &p, const QTextItem &_ti, QText
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawTextItem(), pos=[%.f,%.f], str='%s'\n",
+        fprintf(stderr, "QPainter::drawTextItem(), pos=[%.f,%.f], str='%s'\n",
                p.x(), p.y(), qPrintable(_ti.text()));
 #endif
 
@@ -6641,7 +6656,7 @@ void QPainter::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPo
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::drawTiledPixmap(), target=[%.2f,%.2f,%.2f,%.2f], pix=[%d,%d], offset=[%.2f,%.2f]\n",
+        fprintf(stderr, "QPainter::drawTiledPixmap(), target=[%.2f,%.2f,%.2f,%.2f], pix=[%d,%d], offset=[%.2f,%.2f]\n",
                r.x(), r.y(), r.width(), r.height(),
                pixmap.width(), pixmap.height(),
                sp.x(), sp.y());
@@ -7056,7 +7071,7 @@ void QPainter::setRenderHint(RenderHint hint, bool on)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::setRenderHint: hint=%x, %s\n", hint, on ? "on" : "off");
+        fprintf(stderr, "QPainter::setRenderHint: hint=%x, %s\n", hint, on ? "on" : "off");
 #endif
 
 #ifndef QT_NO_DEBUG
@@ -7169,7 +7184,7 @@ void QPainter::setWindow(const QRect &r)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::setWindow(), [%d,%d,%d,%d]\n", r.x(), r.y(), r.width(), r.height());
+        fprintf(stderr, "QPainter::setWindow(), [%d,%d,%d,%d]\n", r.x(), r.y(), r.width(), r.height());
 #endif
 
     Q_D(QPainter);
@@ -7233,7 +7248,7 @@ void QPainter::setViewport(const QRect &r)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::setViewport(), [%d,%d,%d,%d]\n", r.x(), r.y(), r.width(), r.height());
+        fprintf(stderr, "QPainter::setViewport(), [%d,%d,%d,%d]\n", r.x(), r.y(), r.width(), r.height());
 #endif
 
     Q_D(QPainter);
@@ -7280,7 +7295,7 @@ void QPainter::setViewTransformEnabled(bool enable)
 {
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::setViewTransformEnabled(), enable=%d\n", enable);
+        fprintf(stderr, "QPainter::setViewTransformEnabled(), enable=%d\n", enable);
 #endif
 
     Q_D(QPainter);
@@ -8236,7 +8251,7 @@ void QPainter::resetTransform()
      Q_D(QPainter);
 #ifdef QT_DEBUG_DRAW
     if (qt_show_painter_debug_output)
-        printf("QPainter::resetMatrix()\n");
+        fprintf(stderr, "QPainter::resetMatrix()\n");
 #endif
     if (!d->engine) {
         qWarning("QPainter::resetMatrix: Painter not active");
diff --git a/src/widgets/itemviews/qlistview.cpp b/src/widgets/itemviews/qlistview.cpp
index a17d89e..82b56d1 100644
--- a/src/widgets/itemviews/qlistview.cpp
+++ b/src/widgets/itemviews/qlistview.cpp
@@ -952,6 +952,7 @@ QStyleOptionViewItem QListView::viewOptions() const
 */
 void QListView::paintEvent(QPaintEvent *e)
 {
+    qDebug() << __FUNCTION__ << this;
     Q_D(QListView);
     if (!d->itemDelegate)
         return;
@@ -1061,6 +1062,7 @@ void QListView::paintEvent(QPaintEvent *e)
         painter.restore();
     }
 #endif
+    qDebug() << __FUNCTION__ << '<' << this;
 }
 
 /*!
diff --git a/src/widgets/itemviews/qstyleditemdelegate.cpp b/src/widgets/itemviews/qstyleditemdelegate.cpp
index f02f98c..45ab0fe 100644
--- a/src/widgets/itemviews/qstyleditemdelegate.cpp
+++ b/src/widgets/itemviews/qstyleditemdelegate.cpp
@@ -409,8 +409,10 @@ void QStyledItemDelegate::paint(QPainter *painter,
     initStyleOption(&opt, index);
 
     const QWidget *widget = QStyledItemDelegatePrivate::widget(option);
+    qDebug() << '>' << __FUNCTION__ << "ptr=" << painter << widget;
     QStyle *style = widget ? widget->style() : QApplication::style();
     style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
+    qDebug() << '<' << __FUNCTION__;
 }
 
 /*!
diff --git a/src/widgets/kernel/qwidget.cpp b/src/widgets/kernel/qwidget.cpp
index 7bd4920..b90310d 100644
--- a/src/widgets/kernel/qwidget.cpp
+++ b/src/widgets/kernel/qwidget.cpp
@@ -5462,6 +5462,9 @@ void QWidgetPrivate::drawWidget(QPaintDevice *pdev, const QRegion &rgn, const QP
     bool onScreen = paintOnScreen();
 
     Q_Q(QWidget);
+    qDebug() << __FUNCTION__ << q << ">ST="
+             << "sharedPainter=" << sharedPainter
+             << pdev->paintEngine()->state;
 #ifndef QT_NO_GRAPHICSEFFECT
     if (graphicsEffect && graphicsEffect->isEnabled()) {
         QGraphicsEffectSource *source = graphicsEffect->d_func()->source;
@@ -5473,11 +5476,13 @@ void QWidgetPrivate::drawWidget(QPaintDevice *pdev, const QRegion &rgn, const QP
             if (!sharedPainter) {
                 setSystemClip(pdev, rgn.translated(offset));
                 QPainter p(pdev);
+                qDebug() << __FUNCTION__ << q <<" nonsh painter" << &p;
                 p.translate(offset);
                 context.painter = &p;
                 graphicsEffect->draw(&p);
                 setSystemClip(pdev, QRegion());
             } else {
+                qDebug() << __FUNCTION__ << q <<"sh painter" << sharedPainter;
                 context.painter = sharedPainter;
                 if (sharedPainter->worldTransform() != sourced->lastEffectTransform) {
                     sourced->invalidateCache();
@@ -5516,7 +5521,7 @@ void QWidgetPrivate::drawWidget(QPaintDevice *pdev, const QRegion &rgn, const QP
         if (!onScreen || alsoOnScreen) {
             //update the "in paint event" flag
             if (q->testAttribute(Qt::WA_WState_InPaintEvent))
-                qWarning("QWidget::repaint: Recursive repaint detected");
+                qWarning() << "QWidget::repaint: " << q << "Recursive repaint detected";
             q->setAttribute(Qt::WA_WState_InPaintEvent);
 
             //clip away the new area
@@ -5604,7 +5609,9 @@ void QWidgetPrivate::drawWidget(QPaintDevice *pdev, const QRegion &rgn, const QP
 
             if (!skipPaintEvent) {
                 //actually send the paint event
+                qDebug() << __FUNCTION__ << q  << ">calling sendPaintEvent";
                 sendPaintEvent(toBePainted);
+                qDebug() << __FUNCTION__ << "<called sendPaintEvent";
             }
 
             // Native widgets need to be marked dirty on screen so painting will be done in correct context
@@ -5658,18 +5665,21 @@ void QWidgetPrivate::drawWidget(QPaintDevice *pdev, const QRegion &rgn, const QP
         paintSiblingsRecursive(pdev, children, children.size() - 1, rgn, offset, flags & ~DrawAsRoot
                                 , sharedPainter, backingStore);
     }
+    qDebug() << __FUNCTION__ << q << '<';
 }
 
 void QWidgetPrivate::sendPaintEvent(const QRegion &toBePainted)
 {
-    Q_Q(QWidget);
+    Q_Q(QWidget);    
     QPaintEvent e(toBePainted);
+    qDebug() << __FUNCTION__ << q << "> sending" << &e;
     QCoreApplication::sendSpontaneousEvent(q, &e);
 
 #ifndef QT_NO_OPENGL
     if (renderToTexture)
         resolveSamples();
 #endif // QT_NO_OPENGL
+    qDebug() << __FUNCTION__ << q << '<';
 }
 
 void QWidgetPrivate::render(QPaintDevice *target, const QPoint &targetOffset,
@@ -5870,7 +5880,9 @@ QPixmap QWidgetEffectSourcePrivate::pixmap(Qt::CoordinateSystem system, QPoint *
 
     pixmapOffset -= effectRect.topLeft();
 
-    const qreal dpr = context->painter->device()->devicePixelRatio();
+    const QPaintDevice *pdev = context->painter->device();
+    const qreal dpr = pdev ? pdev->devicePixelRatio() : 1;
+qDebug() << pdev;
     QPixmap pixmap(effectRect.size() * dpr);
     pixmap.setDevicePixelRatio(dpr);
 
@@ -8898,7 +8910,9 @@ bool QWidget::event(QEvent *event)
         // At this point the event has to be delivered, regardless
         // whether the widget isVisible() or not because it
         // already went through the filters
+        qDebug() << __FUNCTION__ << this << ">calling paintEvent " << event;
         paintEvent((QPaintEvent*)event);
+        qDebug() << __FUNCTION__ << this << "<called paintEvent " ;
         break;
 
     case QEvent::Move:
diff --git a/src/widgets/styles/qcommonstyle.cpp b/src/widgets/styles/qcommonstyle.cpp
index 355a3d2..9db35b6 100644
--- a/src/widgets/styles/qcommonstyle.cpp
+++ b/src/widgets/styles/qcommonstyle.cpp
@@ -49,6 +49,7 @@
 #include <qmenu.h>
 #include <qpainter.h>
 #include <qpaintengine.h>
+#include <private/qpaintengine_raster_p.h>
 #include <qpainterpath.h>
 #include <qslider.h>
 #include <qstyleoption.h>
@@ -2139,6 +2140,9 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt,
 #ifndef QT_NO_ITEMVIEWS
     case CE_ItemViewItem:
         if (const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
+
+            QRasterPaintEngine *re = (QRasterPaintEngine *)p->paintEngine();
+            qDebug() << __FUNCTION__ <<  widget << "st=" << re->state();
             p->save();
             p->setClipRect(opt->rect);
 
diff --git a/src/widgets/widgets/qabstractscrollarea.cpp b/src/widgets/widgets/qabstractscrollarea.cpp
index 65d06ea..61a5f7d 100644
--- a/src/widgets/widgets/qabstractscrollarea.cpp
+++ b/src/widgets/widgets/qabstractscrollarea.cpp
@@ -1178,8 +1178,9 @@ bool QAbstractScrollArea::event(QEvent *e)
 bool QAbstractScrollArea::viewportEvent(QEvent *e)
 {
     switch (e->type()) {
-    case QEvent::Resize:
     case QEvent::Paint:
+        qDebug() << __FUNCTION__ << this << e;
+    case QEvent::Resize:    
     case QEvent::MouseButtonPress:
     case QEvent::MouseButtonRelease:
     case QEvent::MouseButtonDblClick:
