Important: Please read the Qt Code of Conduct -

How to apply QStyleSheet Formatting on WhatsThis text

  • Hello,

    I have a file with Qt stylesheet information to change the look of my application. Within that file, I set the way a QToolTip is displayed like this:

      border: 1px solid rgb(64,255,0);
      background: rgb(255,255, 127);
      border-radius 3px;
      padding: 2px;
      color: rgb(67, 67, 67);

    Everything works fine and the tool tips look exactly like I would expect them to look. However, the tips that are displayed in WhatsThisMode are not formatted with this style. QWhatsThis isn't listed among the QWidgets that can be formatted in the Qt documentation but I'm pretty sure there must be a way.

    Has anybody seen this before and has a solution to it?


  • Lifetime Qt Champion

    @tobiSF said in How to apply QStyleSheet Formatting on WhatsThis text:


    Its just a widget
    class QWhatsThat : public QWidget
    but not same window as tooltip

    did you try
    QToolTip, QWhatsThat
    { ..

  • Hi @mrjj,

    I don't think QWhatsThis is a QWidget, at least not with Qt5. I looked up the class declaration and it looks like this:

    #ifndef QWHATSTHIS_H
    #define QWHATSTHIS_H
    #include <QtWidgets/qtwidgetsglobal.h>
    #include <QtCore/qobject.h>
    #include <QtGui/qcursor.h>
    class QAction;
    class Q_WIDGETS_EXPORT QWhatsThis
        QWhatsThis() Q_DECL_EQ_DELETE;
        static void enterWhatsThisMode();
        static bool inWhatsThisMode();
        static void leaveWhatsThisMode();
        static void showText(const QPoint &pos, const QString &text, QWidget *w = Q_NULLPTR);
        static void hideText();
        static QAction *createAction(QObject *parent = Q_NULLPTR);
    #endif // QWHATSTHIS_H

    However, I also did try to assign a style using the combined QToolTip, QWhatsThis{ ... } way and doing separate style definitions, none worked.


  • Lifetime Qt Champion

    In 5.9
    its "C:\Qt\5.9.1\Src\qtbase\src\widgets\kernel\qwhatsthis.cpp"
    class QWhatsThat : public QWidget

    Note the "that"

    so seems to have changed. Or i have wrong file ?
    It seems to match the docs though.

    What version is your code from`?

  • I'm using 5.6 here and I just checked the .cpp file. You're right, it inherits QWidget. Which makes it even less understandable why a simple stylesheet like this wouldn't work:

      background: rgb(255,255,127);
      color(67, 67, 67);

    I am using the public static method enterWhatsThisMode() to put my QDialog into WhatsThis mode and show the respective help texts for each widget in the dialog.

  • Lifetime Qt Champion

    do you apply the stylesheet to Application ?

    I wonder what QWhatsX is a child of. (runtime).

    Also my class is called/ends with That
    Yours are really called QWhatsThis ?

  • @mrjj,
    yes, I have a static method ApplicationStyleManager::loadDefaultStyle() that loads and sets the stylesheet:

    void ApplicationStyleManager::loadDefaultStyle()
    	QSettings settings;
    	QString path = settings.value("style/default.qss").toString();
    		QFile file(path);
    		if (
    			QString styleSheet = QLatin1String(file.readAll());

    I have also taken a closer look at the code. I have a qwhatsthis.h and qwhatsthis.cpp file in my Qt source directory. The header file containts a QWhatsThis class declaration. But the source file contains some other declarations and implementations, among others a QWhatsThat class that inherits QWidget.

    I've tried applying a style in my stylesheet file to QWhatsThat instead of QWhatsThis but with no change.

  • Lifetime Qt Champion

    For test, can you try with QWidget as specifier and see if
    QWhatsThis is affected at all.

    One should think its at least child of the app and should be affected

    Best part is that doc says
    "If you specify a rich text formatted string, it will be rendered using the default stylesheet"

    So clearly stylesheets should work/it knows about it.

    If using no tags, so ALL widgets are colored in whole app, the WhatisThat is not affected.
    So either is not a widget or the window is not attached to the app and hence not using its style or
    it uses a stylesheet internally and override any globals.

  • I actually tried to format the QString I'm setting into the widget like this:

    QString help = QString(tr("<span stlye=\"padding: 3px; border-radius: 5px; border: 1px solid rgb(64,255,0), background: rgb(255,255,127); color: rgb(67, 67, 67)\">Test</span>"));

    However, it did not have any effect.

    I do have a style for QWidget and that's also not affecting the QWhatsThis.

  • Lifetime Qt Champion

    I also tried it.
    Nothing affects it. :)

    I looked in

    Its not using any Styles to draw it and hence cannot be affected as it is not really using the QStyle.
    At least that is how i read it.

    It does use palette.
    p.setPen(QPen(palette().toolTipText(), 0));

  • Lifetime Qt Champion

    so palette will affect it
    QApplication a(argc, argv);
    QPalette p = a.palette();
    p.setColor(QPalette::ColorRole::ToolTipBase, Qt::red);

    does color background red.

  • Ah okay. Not really what I am after, the idea is to load different stylesheets and apply them at runtime of the software. But if that's the only way to get the tip look decent, I guess it's what I'll do.

    Thanks a lot @mrjj

  • Lifetime Qt Champion

    Well its a bit odd it is not made like Tooltip and respect stylesheets.
    You can change text color and background color with palette and the drop shadow also seems to use palette entries.

    you can use palette in stylesheets like
    "background-color : palette(light)"

    but not seen a syntax to set the palette from inside so not sure there is any good path for
    use stylesheets with WhatIsThat, unless you steal/reuse the css parser and
    use it to extract color values and then set the palette for the WhatIS widget.
    (if you need it truly dynamic)

    Np. it was interesting case :)

    Also , i might be wrong :) but in other widgets there is lots of
    QStyle->draw etc
    and WhatIsThat seems to use nothing of that so I assume its the real reason why nothing happens with stylesheets.

  • I agree, it seems like an oversight and I'm going to write an email to support and ask them about it. In the meantime I just use palette. It's not a huge deal with respect to the whole project but I hate when a logical structure behind an architecture has to be altered because of stuff like this.

  • Lifetime Qt Champion

    Sounds like a good plan. Would be great to know the exact details.
    I do understand its far from optimal to have one window that do not
    use same styling system as all the rest.

    It seems not possible to subclass and replace the widget without resort to event filters and
    some amount of copy pasting code from the original widget.
    I mean something like qApp()->setWhatisWidget(xx)

Log in to reply