【问题标题】:How to set the size of a QCheckBox's indicator?如何设置 QCheckBox 指标的大小?
【发布时间】:2018-02-18 10:05:18
【问题描述】:

这段代码:

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

产生:

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


一些实验表明颜色规格会消除尺寸。删除它使尺寸正确,但颜色当然是错误的。我怎样才能两者兼得?

【问题讨论】:

    标签: c++ css qt user-interface


    【解决方案1】:

    试试这个:

    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 download here.提出的问题的示例项目

    【讨论】:

    • 我看到您的两个复选框之间的大小有(非常细微的)差异,但您的代码和我的代码之间根本没有功能差异。我错过了什么吗?
    • 试试看,很明智。设置大小 5px 你可以看得更清楚。
    • 仍然没有区别。见编辑。还尝试了 5px 而不是我想要的值。
    • 还是没有区别。我什至设置了正确的对象吗?我知道几何图形到了正确的位置......
    • 你看到我的截图了吗?
    【解决方案2】:

    感谢@Farhad 最终让我看到这个,但不幸的是,我不能接受他的回答,因为它没有被编辑以包含实际问题。 (不过我确实赞成)所以这就是发生的事情:


    我使用了这段代码,因为“松散”的颜色以前一直有效 - 它们只是应用于对象的每个部分,这对我正在做的事情来说很好:

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

    如您所见,我只是将我的谷歌搜索结果附加到已经工作的内容上......并且添加绝对没有做任何事情,因为全局说明符将其无效。基本上,我创建了一个全局样式,它为除颜色之外的所有内容指定了默认值,它覆盖了indicator 的本地样式。相反,我需要这样做:

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

    基本上,将颜色设置为它们应该应用的局部颜色,这样根本就没有全局样式。


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      相关资源
      最近更新 更多