diff options
author | Frederik Gladhorn <frederik.gladhorn@qt.io> | 2018-10-15 16:39:03 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2019-04-12 14:33:32 +0000 |
commit | 21dcb96ddca357a6e8ace4b1c7252ec465e77727 (patch) | |
tree | 88fdf6336126ad6176dc60984e38428d7549c5f2 | |
parent | 2593463eafcf8b3cb62dd4f8f9545359336bc005 (diff) |
QStyleSheetStyle::repolish: only run on direct children
When re-parenting, some widgets change their children. For example
QLabel, when set to rich text, will not update, until receiving a polish
call, at which time getting a list of all children recursively and then
trying to call functions on them will crash, since the children change
in the middle of this operation.
Fixes: QTBUG-69204
Fixes: QTBUG-74667
Change-Id: I95dd83ebeed14c017e22552ddd47658ae8a09353
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
-rw-r--r-- | src/widgets/styles/qstylesheetstyle.cpp | 5 | ||||
-rw-r--r-- | tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp | 21 |
2 files changed, 25 insertions, 1 deletions
diff --git a/src/widgets/styles/qstylesheetstyle.cpp b/src/widgets/styles/qstylesheetstyle.cpp index 73b147e622..4518d8c736 100644 --- a/src/widgets/styles/qstylesheetstyle.cpp +++ b/src/widgets/styles/qstylesheetstyle.cpp @@ -2905,7 +2905,10 @@ void QStyleSheetStyle::polish(QPalette &pal) void QStyleSheetStyle::repolish(QWidget *w) { - QList<const QObject *> children = w->findChildren<const QObject *>(QString()); + QList<const QObject *> children; + children.reserve(w->children().size() + 1); + for (auto child: qAsConst(w->children())) + children.append(child); children.append(w); styleSheetCaches->styleSheetCache.remove(w); updateObjects(children); diff --git a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp index 0e5c40f1b6..8760ed0373 100644 --- a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp +++ b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp @@ -48,6 +48,7 @@ public: private slots: void init(); void repolish(); + void repolish_without_crashing(); void numinstances(); void widgetsBeforeAppStyleSheet(); void widgetsAfterAppStyleSheet(); @@ -367,6 +368,26 @@ void tst_QStyleSheetStyle::repolish() QCOMPARE(BACKGROUND(p1), APPBACKGROUND(p1)); } +void tst_QStyleSheetStyle::repolish_without_crashing() +{ + // This used to crash, QTBUG-69204 + QMainWindow w; + QScopedPointer<QSplitter> splitter1(new QSplitter(w.centralWidget())); + QScopedPointer<QSplitter> splitter2(new QSplitter); + QScopedPointer<QSplitter> splitter3(new QSplitter); + splitter2->addWidget(splitter3.data()); + + splitter2->setStyleSheet("color: red"); + QScopedPointer<QLabel> label(new QLabel); + label->setTextFormat(Qt::RichText); + splitter3->addWidget(label.data()); + label->setText("hey"); + + splitter1->addWidget(splitter2.data()); + w.show(); + QCOMPARE(COLOR(*label), QColor(Qt::red)); +} + void tst_QStyleSheetStyle::widgetStyle() { qApp->setStyleSheet(""); |