繁体   English   中英

如何设置QCheckBox指标的大小?

[英]How to set the size of a QCheckBox's indicator?

这段代码:

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));

生产:

在此处输入图片说明

白色区域具有正确的几何形状,但是我希望指示器框(以及单击目标)充满它。 我怎么做?


进行一些实验表明,颜色规格消除了尺寸。 删除它可以使尺寸正确,但是颜色当然是错误的。 我怎么都可以?

尝试这个:

ui->checkBox->setStyleSheet("QCheckBox::indicator {width:10px;height: 10px;}");

您必须为每个状态手动设置图标,如下所示:

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);
}

图片截图: 图片

这是您在github上下载的问题的示例项目

感谢@aghilpro最终使我明白了这一点,但是不幸的是,我无法接受他的回答,因为尚未对其进行编辑以包括实际问题。 (尽管我确实对此表示赞同),所以这是发生的事情:


我之所以使用此代码,是因为“松散”的颜色以前一直奏效-它们仅适用于对象的每个部分,这对我所做的事情是很好的:

QCheckBox* selection = new QCheckBox(backgroundEditor);
selection->setStyleSheet(QString(
                      "background-color: white;"
                      "           color: black;"
                      "QCheckBox::indicator "
                      "{"
                      "     width: %1px;"
                      "    height: %1px;"
                      "}"
                      ).arg(buttonHeight));

如您所见,我只是将我的google搜索结果附加到已经起作用的内容上...添加的内容完全没有任何作用,因为全局说明符使它无效了。 基本上,我创建了一种全局样式,该样式指定了除颜色以外的所有内容的默认值,并且它覆盖了indicator的本地颜色。 相反,我需要这样做:

QCheckBox* selection = new QCheckBox(backgroundEditor);
selection->setStyleSheet(QString(
                      "QCheckBox"
                      "{"
                      "    background-color: white;"
                      "               color: black;"
                      "}"
                      "QCheckBox::indicator"
                      "{"
                      "     width: %1px;"
                      "    height: %1px;"
                      "}"
                      ).arg(buttonHeight));

基本上,将颜色设置为应该应用的颜色,从而完全没有全局样式。


如果某人对实际发生的事情有更好的了解,请告诉我,以便我可以解决此问题,或者编写您自己的更好的答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM