Avoid potential crash when changing styles with stylesheet

This is not always reproducible, but it probably crashed because
QStyleSheetStyle::polish() can do some nasty things such as creating
and deleting objects, which can leave the list with dangling QObject
pointers.

However, it should not delete QWidgets, so we make sure we only have
a list of QWidgets before we perform the polish iteration.

Change-Id: I84c1ca6a7316e72348248ff056b65dcbae3d20a3
Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com>
This commit is contained in:
Jan Arve Saether 2013-02-19 12:09:12 +01:00 committed by The Qt Project
parent b6ccdfa482
commit 7508719830

View File

@ -2605,14 +2605,17 @@ static void updateObjects(const QList<const QObject *>& objects)
styleSheetCaches->renderRulesCache.remove(object);
}
}
for (int i = 0; i < objects.size(); ++i) {
QObject *object = const_cast<QObject *>(objects.at(i));
if (object == 0)
continue;
if (QWidget *widget = qobject_cast<QWidget *>(object))
widget->style()->polish(widget);
QEvent event(QEvent::StyleChange);
QApplication::sendEvent(object, &event);
QWidgetList widgets;
foreach (const QObject *object, objects) {
if (QWidget *w = qobject_cast<QWidget*>(const_cast<QObject*>(object)))
widgets << w;
}
QEvent event(QEvent::StyleChange);
foreach (QWidget *widget, widgets) {
widget->style()->polish(widget);
QApplication::sendEvent(widget, &event);
}
}