SetStyleSheet and system style (font)
-
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.- why it is happens?
- why the font of QCheckBox is not reseted with FIRST call of setStyleSheet?
- 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)! -
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.
-
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.
-
[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...)?
-
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