我正在处理一个示例应用程序,其中需要禁用复选框的文本,但未选中该复选框并在选中时启用它。禁用/启用QCheckBox文本
代码:
if(ui->checkBox->isChecked() == true)
{
// Enable the text of the checkbox
}
else
{
// Disable the text of the checkbox
}
我已经通过各种文章看了,但我还没有找到合适的解决方案。
我正在处理一个示例应用程序,其中需要禁用复选框的文本,但未选中该复选框并在选中时启用它。禁用/启用QCheckBox文本
代码:
if(ui->checkBox->isChecked() == true)
{
// Enable the text of the checkbox
}
else
{
// Disable the text of the checkbox
}
我已经通过各种文章看了,但我还没有找到合适的解决方案。
对于所有窗口小部件:
ui->setStyleSheet(
"QCheckBox:checked {
{color: black;}
}
QCheckBox:unchecked {
{color: grey;}
}"
)
编辑:
正如在评论中提及,使其与自定义主题,则可以使样式查询palette :
QPalette my_palette = ui->palette()
QColor my_active_color = my_palette.color(QPalette::Active, QPalette::Text);
QColor my_disabled_color = my_palette.color(QPalette::Disabled, QPalette::Text);
QString my_style =
QString("QCheckBox:checked { {color: rgb(%1, %2, %3);} } "
"QCheckBox:unchecked { {color: rgb(%4, %5, %6);}}")
.arg(my_active_color.red())
.arg(my_active_color.green())
.arg(my_active_color.blue())
.arg(my_disabled_color.red())
.arg(my_disabled_color.green())
.arg(my_disabled_color.blue());
ui->setStyleSheet(my_style);
请注意,id没有尝试过,它可能有任何错字,但你明白了。
如果用户使用白色文本的自定义配色方案会发生什么?如果你蒙皮一些小部件,你应该整个应用程序的皮肤,使一切都是一致的。此处不能使用硬编码颜色,应从当前调色板中获取启用和禁用的文本颜色。 – Paul 2013-04-24 11:15:43
@Trompa:我试过了:'ui-> checkBox-> setStyleSheet(“QCheckBox :: indicator:checked {{color:black;}} QCheckBox :: indicator:unchecked {{color:grey;}}”);'但它似乎并没有工作..... – Gojira 2013-04-24 11:17:33
感谢您的帮助Trompa :) – Gojira 2013-04-24 11:29:50
我得到了解决方案。这是我如何实现它:
ui->checkBox->setStyleSheet("QCheckBox:checked{color: black;} QCheckBox:unchecked{color: grey;}");
所以,你不需要指标部分...坏Qt文档...我会编辑我的答案,然后,以防其他人来解决这个问题 – Trompa 2013-04-24 12:46:41
你是什么意思的“禁用文本”,它是使文本“灰色”? – 2013-04-24 11:08:31
@ YamHon.CHAN:是的......基本上灰色的文字 – Gojira 2013-04-24 11:09:21