[英]How to set the size of a QCheckBox's indicator?
This code: 这段代码:
QCheckBox* selection = new QCheckBox(backgroundEditor);
selection->setGeometry(
0 , //Left
0 , //Top
buttonHeight , //Width
buttonHeight //Height
);
selection->setStyleSheet(QString(
"background-color: white;"
" color: black;"
"QCheckBox::indicator "
"{"
" width: %1px;"
" height: %1px;"
"}"
).arg(buttonHeight));
produces: 生产:
The white area has the correct geometry, but I want the indicator box (and thus the click-target) to fill it. 白色区域具有正确的几何形状,但是我希望指示器框(以及单击目标)充满它。 How do I do that?
我怎么做?
A bit of experimenting shows that the color specification wipes out the size. 进行一些实验表明,颜色规格消除了尺寸。 Removing it makes the size correct, but of course the colors are wrong.
删除它可以使尺寸正确,但是颜色当然是错误的。 How can I have both?
我怎么都可以?
Try this: 尝试这个:
ui->checkBox->setStyleSheet("QCheckBox::indicator {width:10px;height: 10px;}");
you have to set icon manually for each state like this: 您必须为每个状态手动设置图标,如下所示:
QCheckBox::indicator:checked
{
image: url(../Checkbox_checked_normal.png);
}
QCheckBox::indicator:unchecked
{
image: url(../Checkbox_unchecked_normal.png);
}
QCheckBox::indicator:checked:hover
{
image: url(../Checkbox_checked_hovered.png);
}
QCheckBox::indicator:unchecked:hover
{
image: url(../Checkbox_unchecked_hovered.png);
}
QCheckBox::indicator:checked:pressed
{
image: url(../Checkbox_checked_pressed.png);
}
QCheckBox::indicator:unchecked:pressed
{
image: url(../Checkbox_unchecked_pressed.png);
}
QCheckBox::indicator:checked:disabled
{
image: url(../Checkbox_checked_disabled.png);
}
This is a sample project for your question on github download here. 这是您在github上下载的问题的示例项目。
Thanks @aghilpro for eventually getting me to see this, but unfortunately, I can't accept his answer because it hasn't been edited to include the actual problem. 感谢@aghilpro最终使我明白了这一点,但是不幸的是,我无法接受他的回答,因为尚未对其进行编辑以包括实际问题。 (I did upvote it though) So here's what happened:
(尽管我确实对此表示赞同),所以这是发生的事情:
I had used this code because "loose" colors have always worked before - they just applied to every part of the object, which was fine for what I was doing: 我之所以使用此代码,是因为“松散”的颜色以前一直奏效-它们仅适用于对象的每个部分,这对我所做的事情是很好的:
QCheckBox* selection = new QCheckBox(backgroundEditor);
selection->setStyleSheet(QString(
"background-color: white;"
" color: black;"
"QCheckBox::indicator "
"{"
" width: %1px;"
" height: %1px;"
"}"
).arg(buttonHeight));
As you can see, I just appended my google search results onto what had already worked...and the addition did absolutely nothing because the global specifiers nullified it. 如您所见,我只是将我的google搜索结果附加到已经起作用的内容上...添加的内容完全没有任何作用,因为全局说明符使它无效了。 Basically, I created a global style that specified the defaults for everything except color, and it overrode the
indicator
's local one. 基本上,我创建了一种全局样式,该样式指定了除颜色以外的所有内容的默认值,并且它覆盖了
indicator
的本地颜色。 Instead, I needed to do this: 相反,我需要这样做:
QCheckBox* selection = new QCheckBox(backgroundEditor);
selection->setStyleSheet(QString(
"QCheckBox"
"{"
" background-color: white;"
" color: black;"
"}"
"QCheckBox::indicator"
"{"
" width: %1px;"
" height: %1px;"
"}"
).arg(buttonHeight));
Basically, make the colors local to what they're supposed to apply to so that there's no global style at all. 基本上,将颜色设置为应该应用的颜色,从而完全没有全局样式。
If someone has a better insight as to what's actually happening, please let me know so I can fix this answer, or write your own better answer. 如果某人对实际发生的事情有更好的了解,请告诉我,以便我可以解决此问题,或者编写您自己的更好的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.