QCheckBox background
-
Hello,
I have a problem to set the QCheckBox's background color. The following code sets the background correctly but only when mouse is over the checkbox:@QColor col(255, 0, 0);
QPalette p = checkbox.palette();
p.setColor(QPalette::Background, col);
checkbox.setPalette(p);
@which produces something like this:
!http://www.imagecode.eu/checkbox.png!But how to set the background when the mouse is not over the checkbox? I'd like to avoid to write my own custom drawn checkbox. I'm sure it must be possible...
Note that my application uses the QCleanLookStyle. When I use default style then the background doesn't work anyway (neither when the mouse is over nor the mouse is not over).
Thanks in advance!
-
Hi LairMan,
Welcome to DevNet.
You can use "qss":http://qt-project.org/doc/qt-5/stylesheet-examples.html for such things. -
Have you tried using a more explicit "setColor":http://qt-project.org/doc/qt-5/qpalette.html#setColor method? Maybe set your color for the active as well as the inactive state.
If things don't work out, just use a stylesheet on that checkbox:
@checkbox.setStyleSheet("QCheckBox { background: red; }"; //I didn't look up the exact property to set, but the documentation is quite extensive@ -
qxoz:
using the stylesheet I get this result:
!http://www.imagecode.eu/checkbox2.png!The square is colored too, but I don't like it, I'd like to keep it white (or some default color).
thEClaw:
I tried the active and inactive (and many others) but it not worked :-( -
If the result of using the stylesheet is not ok, you'll have to read more of the documentation and change more than the background color. There is probably a sub-control for the little box, or maybe a special property.
-
[quote author="LairMan" date="1395315820"]qxoz:
using the stylesheet I get this result:
!http://www.imagecode.eu/checkbox2.png!
[/quote]
hm.. very strange.
That what i got:
!http://s10.postimg.org/jqn1m60fd/Untitled.png(example)!
Probably it's because you use QCleanLookStyle.
If yes and you can't use default style you can set indicator as image (look at "qss examples":http://qt-project.org/doc/qt-5/stylesheet-examples.html) -
Ok, it seems that there is no easy way :). I'll set the indicator as image, it should work.
Thanks to all. -
There should be an easy way, just take a look into the documentation:
QCheckBox:
bq. Supports the box model. The check indicator can be styled using the ::indicator subcontrol. By default, the indicator is placed in the Top Left corner of the Contents rectangle of the widget.