OSDN Git Service

partially revert 42245e22d7f91ea369726b54d22d47f2da60365d
authorIvailo Monev <xakepa10@gmail.com>
Fri, 18 Jun 2021 13:53:28 +0000 (16:53 +0300)
committerIvailo Monev <xakepa10@gmail.com>
Fri, 18 Jun 2021 13:53:28 +0000 (16:53 +0300)
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
src/gui/kernel/qtooltip.cpp
src/gui/styles/qstylesheetstyle.cpp
tests/auto/qstylesheetstyle/tst_qstylesheetstyle.cpp

index 5e179ea..3df462a 100644 (file)
@@ -116,6 +116,20 @@ protected:
     void mouseMoveEvent(QMouseEvent *e);
     void resizeEvent(QResizeEvent *e);
 
+#ifndef QT_NO_STYLE_STYLESHEET
+public slots:
+    /** \internal
+      Cleanup the _q_stylesheet_parent propery.
+     */
+    void styleSheetParentDestroyed() {
+        setProperty("_q_stylesheet_parent", QVariant());
+        styleSheetParent = 0;
+    }
+
+private:
+    QWidget *styleSheetParent;
+#endif
+
 private:
     QWidget *widget;
     QRect rect;
@@ -124,7 +138,11 @@ private:
 QTipLabel *QTipLabel::instance = 0;
 
 QTipLabel::QTipLabel(const QString &text, QWidget *w)
+#ifndef QT_NO_STYLE_STYLESHEET
+    : QLabel(w, Qt::ToolTip | Qt::BypassGraphicsProxyWidget), styleSheetParent(0), widget(0)
+#else
     : QLabel(w, Qt::ToolTip | Qt::BypassGraphicsProxyWidget), widget(0)
+#endif
 {
     delete instance;
     instance = this;
@@ -150,6 +168,14 @@ void QTipLabel::restartExpireTimer()
 
 void QTipLabel::reuseTip(const QString &text)
 {
+#ifndef QT_NO_STYLE_STYLESHEET
+    if (styleSheetParent){
+        disconnect(styleSheetParent, SIGNAL(destroyed()),
+                   QTipLabel::instance, SLOT(styleSheetParentDestroyed()));
+        styleSheetParent = 0;
+    }
+#endif
+
     setWordWrap(Qt::mightBeRichText(text));
     setText(text);
     QFontMetrics fm(font());
@@ -269,8 +295,18 @@ int QTipLabel::getTipScreen(const QPoint &pos, QWidget *w)
 void QTipLabel::placeTip(const QPoint &pos, QWidget *w)
 {
 #ifndef QT_NO_STYLE_STYLESHEET
-    if (w) {
-        setStyleSheet(w->styleSheet());
+    if (testAttribute(Qt::WA_StyleSheet) || (w && qobject_cast<QStyleSheetStyle *>(w->style()))) {
+        //the stylesheet need to know the real parent
+        QTipLabel::instance->setProperty("_q_stylesheet_parent", QVariant::fromValue(w));
+        //we force the style to be the QStyleSheetStyle, and force to clear the cache as well.
+        QTipLabel::instance->setStyleSheet(QLatin1String("/* */"));
+
+        // Set up for cleaning up this later...
+        QTipLabel::instance->styleSheetParent = w;
+        if (w) {
+            connect(w, SIGNAL(destroyed()),
+                QTipLabel::instance, SLOT(styleSheetParentDestroyed()));
+        }
     }
 #endif //QT_NO_STYLE_STYLESHEET
 
index ecc1120..7d5d5b7 100644 (file)
@@ -1377,6 +1377,16 @@ void QRenderRule::configurePalette(QPalette *p, QPalette::ColorGroup cg, const Q
 // Style rules
 #define WIDGET(x) (static_cast<QWidget *>(x.ptr))
 
+static inline QWidget *parentWidget(const QWidget *w)
+{
+    if(qobject_cast<const QLabel *>(w) && qstrcmp(w->metaObject()->className(), "QTipLabel") == 0) {
+        QWidget *p = qvariant_cast<QWidget *>(w->property("_q_stylesheet_parent"));
+        if (p)
+            return p;
+    }
+    return w->parentWidget();
+}
+
 class QStyleSheetStyleSelector : public StyleSelector
 {
 public:
@@ -1463,7 +1473,7 @@ public:
     bool isNullNode(NodePtr node) const
     { return node.ptr == 0; }
     NodePtr parentNode(NodePtr node) const
-    { NodePtr n; n.ptr = isNullNode(node) ? 0 : WIDGET(node)->parentWidget(); return n; }
+    { NodePtr n; n.ptr = isNullNode(node) ? 0 : parentWidget(WIDGET(node)); return n; }
     NodePtr previousSiblingNode(NodePtr) const
     { NodePtr n; n.ptr = 0; return n; }
     NodePtr duplicateNode(NodePtr node) const
@@ -1516,7 +1526,7 @@ QVector<QCss::StyleRule> QStyleSheetStyle::styleRules(const QWidget *w) const
     }
 
     QVector<QCss::StyleSheet> widgetSs;
-    for (const QWidget *wid = w; wid; wid = wid->parentWidget()) {
+    for (const QWidget *wid = w; wid; wid = parentWidget(wid)) {
         if (wid->styleSheet().isEmpty())
             continue;
         StyleSheet ss;
index f2c970d..123b3e5 100644 (file)
@@ -1280,8 +1280,6 @@ private:
 
 void tst_QStyleSheetStyle::toolTip()
 {
-    QSKIP("FIXME: test fails, investigate", SkipAll);
-
     qApp->setStyleSheet(QString());
     QWidget w;
     QHBoxLayout layout(&w);
@@ -1338,6 +1336,7 @@ void tst_QStyleSheetStyle::toolTip()
                 break;
             }
         }
+
         QVERIFY(tooltip);
         QCOMPARE(tooltip->palette().color(tooltip->backgroundRole()), col);
     }