QGV: set QStyleOption::styleObject for graphics items & widgets
authorJ-P Nurmi <jpnurmi@digia.com>
Thu, 29 Nov 2012 22:02:52 +0000 (23:02 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Fri, 30 Nov 2012 18:32:35 +0000 (19:32 +0100)
Certain QStyle code paths expect the style object to be set. This will
avoid problems when QGraphicsItems/Widgets utilize QStyle for drawing.
Even if things will not necessarily animate properly, such legacy code
won't cause a crash at least.

Change-Id: Ic77cb4ee9820d6c4ec92c7278fd87cb866f5a780
Reviewed-by: Andreas Aardal Hanssen <andreas@hanssen.name>
Reviewed-by: Morten Johan Sørvig <morten.sorvig@digia.com>
src/widgets/graphicsview/qgraphicsitem.cpp
src/widgets/graphicsview/qgraphicsscene.cpp
src/widgets/graphicsview/qgraphicswidget.cpp

index 70b4442..c30435e 100644 (file)
@@ -1315,6 +1315,14 @@ void QGraphicsItemPrivate::initStyleOption(QStyleOptionGraphicsItem *option, con
     option->rect = brect.toRect();
     option->levelOfDetail = 1;
     option->exposedRect = brect;
+
+    // Style animations require a QObject-based animation target.
+    // If a plain QGraphicsItem is used to draw animated controls,
+    // QStyle is let to send animation updates to the whole scene.
+    option->styleObject = q_ptr->toGraphicsObject();
+    if (!option->styleObject)
+        option->styleObject = scene;
+
     if (selected)
         option->state |= QStyle::State_Selected;
     if (enabled)
index 3116257..ee65fb8 100644 (file)
@@ -3432,6 +3432,11 @@ bool QGraphicsScene::event(QEvent *event)
         // geometries that do not have an explicit style set.
         update();
         break;
+    case QEvent::StyleAnimationUpdate:
+        // Because QGraphicsItem is not a QObject, QStyle driven
+        // animations are forced to update the whole scene
+        update();
+        break;
     case QEvent::TouchBegin:
     case QEvent::TouchUpdate:
     case QEvent::TouchEnd:
index 859f07a..c94d027 100644 (file)
@@ -734,6 +734,7 @@ void QGraphicsWidget::initStyleOption(QStyleOption *option) const
         option->palette.setCurrentColorGroup(QPalette::Inactive);
     }
     option->fontMetrics = QFontMetrics(font());
+    option->styleObject = const_cast<QGraphicsWidget *>(this);
 }
 
 /*!