Ensure that all the children get the new stylesheet set on a parent
authorAndy Shaw <andy.shaw@digia.com>
Tue, 7 Aug 2012 04:31:38 +0000 (06:31 +0200)
committerQt by Nokia <qt-info@nokia.com>
Mon, 20 Aug 2012 08:28:51 +0000 (10:28 +0200)
When a stylesheet was set on a parent widget then in some cases it would
not get applied to all the child widgets.  This was because the order of
the children list may have been modified while it was being set on
children.  By making a copy of the list we prevent this from being a
problem.

Task-number: QTBUG-26321

Change-Id: Iea6bf72c69a0c39746f7ef5e7893dda5a93ed7e5
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@digia.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@nokia.com>
src/widgets/kernel/qwidget.cpp
tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp

index 9b4a4bd..d41e80f 100644 (file)
@@ -2472,8 +2472,10 @@ void QWidgetPrivate::setStyle_helper(QStyle *newStyle, bool propagate, bool
     }
 
     if (propagate) {
-        for (int i = 0; i < children.size(); ++i) {
-            QWidget *c = qobject_cast<QWidget*>(children.at(i));
+        // We copy the list because the order may be modified
+        const QObjectList childrenList = children;
+        for (int i = 0; i < childrenList.size(); ++i) {
+            QWidget *c = qobject_cast<QWidget*>(childrenList.at(i));
             if (c)
                 c->d_func()->inheritStyle();
         }
index f38a124..0c35107 100644 (file)
@@ -72,7 +72,7 @@
 #include <qtableview.h>
 #include <qtreewidget.h>
 #include <qabstractnativeeventfilter.h>
-
+#include <qproxystyle.h>
 #include <QtWidgets/QGraphicsView>
 #include <QtWidgets/QGraphicsProxyWidget>
 
@@ -387,6 +387,7 @@ private slots:
 
     void touchEventSynthesizedMouseEvent();
 
+    void styleSheetPropagation();
 private:
     bool ensureScreenSize(int width, int height);
     QWidget *testWidget;
@@ -9513,5 +9514,15 @@ void tst_QWidget::touchEventSynthesizedMouseEvent()
     }
 }
 
+void tst_QWidget::styleSheetPropagation()
+{
+    QTableView tw;
+    tw.setStyleSheet("background-color: red;");
+    foreach (QObject *child, tw.children()) {
+        if (QWidget *w = qobject_cast<QWidget *>(child))
+            QCOMPARE(w->style(), tw.style());
+    }
+}
+
 QTEST_MAIN(tst_QWidget)
 #include "tst_qwidget.moc"