Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SetStyleSheet and system style (font)
Qt 6.12 LTS is out! See what's new in the release blog

SetStyleSheet and system style (font)

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 6.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I have a dialog with a checkbox QCheckBox. And I want to have different colors of the checkbox's text at its checked and unchecked states. No problem, the color is changed with setStyleSheet() in the corresponding slot onClicked().
    Also I want to specify the font (family and size) of my dialog and all its childs (in the sample below - only checkbox). Well, I do that in a dialog constructor with setFont(), and the font of my checkbox also changed. Good.
    And there is a problem. With the second and following call of setStyleSheet() in the slot onClicked(), the font of QCheckBox is reseting to system default.

    1. why it is happens?
    2. why the font of QCheckBox is not reseted with FIRST call of setStyleSheet?
    3. Currently I solved my problem, manually setting a font of QCheckBox (commented string in the code below). Can you suggest the better approach?

    @
    #include "dialog.h"

    dialog::dialog(QWidget *parent) :
    QWidget(parent)
    {
    resize(150, 50);
    QFont font;
    font.setFamily(QStringLiteral("Arial"));
    font.setPointSize(14);
    setFont(font);

    checkBox = new QCheckBox("Checkbox", this);
    checkBox->setGeometry(QRect(0, 0, 150, 25));
    //checkBox->setFont(font);
    
    connect(checkBox, SIGNAL(clicked(bool)), this, SLOT(onClicked(bool)));
    

    }

    void dialog::onClicked(bool checked)
    {
    if(checked)
    checkBox->setStyleSheet("color: red;");
    else
    checkBox->setStyleSheet("color: blue;");
    }
    @

    Screenshots:
    !http://i.imgur.com/FAyM2gh.png?1(screenshots)!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      adutzu89
      wrote on last edited by
      #2

      May I recommend the following way for setting you checked/unchecked states colors of your checkbox:

      @QCheckBox::checked {
      //how it should be in checked state
      }
      QCheckBox::unchecked {
      //how it should be in unchecked state
      }@

      Also try setting the font size with stylesheets and see if the problem persists.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        To add to adutzu89, when using style sheet, you should set all customization you'd like in there. Styled widgets are not rendered through the same style plugin.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          [quote author="adutzu89" date="1418830368"]May I recommend the following way for setting you checked/unchecked states colors of your checkbox:[/quote]

          Thank you, adutzu89, I changed code:
          @
          checkBox = new QCheckBox("Checkbox", this);
          checkBox->setGeometry(QRect(0, 0, 150, 25));
          checkBox->setStyleSheet("QCheckBox::unchecked {color: blue } QCheckBox::checked {color: yellow };");
          //connect(checkBox, SIGNAL(clicked(bool)), this, SLOT(onClicked(bool)));
          @
          so it contains only one call to setStyleSheet() and it works fine (font family and size aren't changing).

          But ultimately I need to colour not only checkboxes but also labels, in order to mark user input error: (see sreenshot).
          !http://i.imgur.com/EiBsLof.png(screenshot)!
          So for QLabels I try your second advice:
          [quote author="adutzu89" date="1418830368"] Also try setting the font size with stylesheets and see if the problem persists.
          [/quote]
          It's also works fine:
          @
          void dialog::onClicked(bool checked)
          {
          if(checked)
          checkBox->setStyleSheet("color: red; font-family: Arial; font-size: 14pt;");
          else
          checkBox->setStyleSheet("color: blue; font-family: Arial; font-size: 14pt;");
          }
          @
          But it resembles crutches, because I need to define here all differences between mine and system styles...

          [quote author="SGaist" date="1418850599"]To add to adutzu89, when using style sheet, you should set all customization you'd like in there. Styled widgets are not rendered through the same style plugin.[/quote]

          Thank you for answer... Nevertheless, why the problem (resetting to system style) doesn't appear when I use only one call to setStyleSheet() (like in first portion of code in this post...)?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adutzu89
            wrote on last edited by
            #5

            Sorry for the late answer.

            I do not understand what you are trying to say here.

            bq. It’s also works fine:

            @void dialog::onClicked(bool checked)
            {
            if(checked)
            checkBox->setStyleSheet("color: red; font-family: Arial; font-size: 14pt;");
            else
            checkBox->setStyleSheet("color: blue; font-family: Arial; font-size: 14pt;");
            }@

            bq. But it resembles crutches, because I need to define here all differences between mine and system styles…

            If this example works fine:

            @void dialog::onClicked(bool checked)
            {
            if(checked)
            checkBox->setStyleSheet("color: red; font-family: Arial; font-size: 14pt;");
            else
            checkBox->setStyleSheet("color: blue; font-family: Arial; font-size: 14pt;");
            }@

            You can adjust it to include all items you need to modify, something like this:

            @void dialog::onClicked(bool checked)
            {
            if(checked)
            parent->setStyleSheet("QCheckBox,QLabel {color: red; font-family: Arial; font-size: 14pt;}");
            else
            parent->setStyleSheet("QCheckBox,QLabel {color: blue; font-family: Arial; font-size: 14pt;}");
            }@

            Where "parent" is the widget containing the checkbox,labels and other widgets which needs styling, you can also target a object specifying it with a "#".
            I will use your example for selecting the object for styling:

            @this->setStyleSheet("QCheckBox#checkBox { /*style in here to target checkBox widget without affecting other QCheckBox widgets */ }" )@

            Look here for more in-depth info on Qts stylesheets system

            "http://qt-project.org/doc/qt-4.8/stylesheet-examples.html":http://qt-project.org/doc/qt-4.8/stylesheet-examples.html

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt
            • Unsolved