1diff --git a/src/gui/kernel/qwidget.cpp b/src/gui/kernel/qwidget.cpp
2index 030e31b..0acd621e 100644
3--- a/src/gui/kernel/qwidget.cpp
4+++ b/src/gui/kernel/qwidget.cpp
5@@ -2739,7 +2739,7 @@ void QWidget::setStyle(QStyle *style)
6         //(this may happen for exemple in QButtonDialogBox which propagates its style)
7         proxy->ref();
8         d->setStyle_helper(style, false);
9-    } else if (qobject_cast<QStyleSheetStyle *>(d->extra->style) || !qApp->styleSheet().isEmpty()) {
10+    } else if (qobject_cast<QStyleSheetStyle *>(d->extra->style) || (style && !qApp->styleSheet().isEmpty())) {
11         // if we have an application stylesheet or have a proxy already, propagate
12         d->setStyle_helper(new QStyleSheetStyle(style), true);
13     } else
14@@ -2800,7 +2800,8 @@ void QWidgetPrivate::setStyle_helper(QStyle *newStyle, bool propagate, bool
15     }
16
17 #ifndef QT_NO_STYLE_STYLESHEET
18-    if (!qobject_cast<QStyleSheetStyle*>(newStyle)) {
19+    // if we are done using stylesheet style, tidy up
20+    if (!qobject_cast<QStyleSheetStyle*>(newStyle) && qApp->styleSheet().isEmpty()) {
21         if (const QStyleSheetStyle* cssStyle = qobject_cast<QStyleSheetStyle*>(origStyle.data())) {
22             cssStyle->clearWidgetFont(q);
23         }
24@@ -2838,13 +2839,16 @@ void QWidgetPrivate::inheritStyle()
25     QWidget *parent = q->parentWidget();
26     QStyle *parentStyle = (parent && parent->d_func()->extra) ? (QStyle*)parent->d_func()->extra->style : 0;
27     // If we have stylesheet on app or parent has stylesheet style, we need
28-    // to be running a proxy
29+    // to be running a proxy. Although, stylesheet on app alone doesn't make
30+    // us need proxy here.
31     if (!qApp->styleSheet().isEmpty() || qobject_cast<QStyleSheetStyle *>(parentStyle)) {
32         QStyle *newStyle = parentStyle;
33         if (q->testAttribute(Qt::WA_SetStyle))
34             newStyle = new QStyleSheetStyle(origStyle);
35         else if (QStyleSheetStyle *newProxy = qobject_cast<QStyleSheetStyle *>(parentStyle))
36             newProxy->ref();
37+        // else newStyle is surely 0, because if app has style sheet,
38+        // our parent can't possibly have a non-proxy style
39
40         setStyle_helper(newStyle, true);
41         return;
42@@ -4829,9 +4833,8 @@ void QWidget::setFont(const QFont &font)
43     Q_D(QWidget);
44
45 #ifndef QT_NO_STYLE_STYLESHEET
46-    const QStyleSheetStyle* style;
47-    if (d->extra && (style = qobject_cast<const QStyleSheetStyle*>(d->extra->style))) {
48-        style->saveWidgetFont(this, font);
49+    if (QStyleSheetStyle *cssStyle = qobject_cast<QStyleSheetStyle *>(style())) {
50+        cssStyle->saveWidgetFont(this, font);
51     }
52 #endif
53
54@@ -4919,8 +4922,7 @@ void QWidgetPrivate::updateFont(const QFont &font)
55 {
56     Q_Q(QWidget);
57 #ifndef QT_NO_STYLE_STYLESHEET
58-    const QStyleSheetStyle* cssStyle;
59-    cssStyle = extra ? qobject_cast<const QStyleSheetStyle*>(extra->style) : 0;
60+    QStyleSheetStyle *cssStyle = qobject_cast<QStyleSheetStyle *>(q->style());
61 #endif
62
63 #ifdef QT3_SUPPORT
64diff --git a/src/gui/styles/qstylesheetstyle.cpp b/src/gui/styles/qstylesheetstyle.cpp
65index 9eaafb3..9b1fe6f 100644
66--- a/src/gui/styles/qstylesheetstyle.cpp
67+++ b/src/gui/styles/qstylesheetstyle.cpp
68@@ -2551,8 +2551,12 @@ void QStyleSheetStyle::setPalette(QWidget *w)
69                 saveWidgetFont(w, w->font());
70             }
71             updateStyleSheetFont(w);
72-            if (ew != w)
73+            if (ew != w) {
74+                if (!ew->property("_q_styleSheetWidgetFont").isValid()) {
75+                    saveWidgetFont(ew, ew->font());
76+                }
77                 updateStyleSheetFont(ew);
78+            }
79         }
80
81         rule.configurePalette(&p, map[i].group, ew, ew != w);
82@@ -2566,17 +2570,21 @@ void QStyleSheetStyle::setPalette(QWidget *w)
83
84 void QStyleSheetStyle::unsetPalette(QWidget *w)
85 {
86+    QWidget *ew = embeddedWidget(w);
87     if (styleSheetCaches->customPaletteWidgets.contains(w)) {
88         QPalette p = styleSheetCaches->customPaletteWidgets.value(w);
89         w->setPalette(p);
90-        QWidget *ew = embeddedWidget(w);
91         if (ew != w)
92             ew->setPalette(p);
93         styleSheetCaches->customPaletteWidgets.remove(w);
94     }
95     QVariant oldFont = w->property("_q_styleSheetWidgetFont");
96-    if (oldFont.isValid()) {
97+    if (oldFont.isValid())
98         w->setFont(qvariant_cast<QFont>(oldFont));
99+    if (ew != w) {
100+        oldFont = ew->property("_q_styleSheetWidgetFont");
101+        if (oldFont.isValid())
102+            ew->setFont(qvariant_cast<QFont>(oldFont));
103     }
104     if (styleSheetCaches->autoFillDisabledWidgets.contains(w)) {
105         embeddedWidget(w)->setAutoFillBackground(true);
106@@ -5805,6 +5813,12 @@ void QStyleSheetStyle::updateStyleSheetFont(QWidget* w) const
107             PseudoClass_Active | PseudoClass_Enabled | extendedPseudoClass(container));
108     QFont font = rule.font.resolve(w->font());
109
110+    // To not forget what was explicitly set in the widget font,
111+    // we need to restore the mask. Otherwise a natural child
112+    // could forget its custom font and get one from its parent
113+    // when restoring font after style sheet has been removed.
114+    font.resolve(font.resolve() | w->font().resolve());
115+
116     if ((!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))
117         && isNaturalChild(w) && qobject_cast<QWidget *>(w->parent())) {
118
119