将自定义颜色的按钮设置为禁用样式 [英] Set a button of custom color to a disabled style
问题描述
我有一个继承 QWidget
的类.在某些情况下,我希望将其设置为禁用.
I have a class that inherits QWidget
. Under certain circumstances, I wish to set it disabled.
小部件有一些颜色按钮,它们是从颜色列表中设置的:
The widget has some color buttons, that are set from a list of colors:
void MyWidget::colorUpdate(QString color)
{
if(!color.isEmpty())
{
QString foreground = (QColor(color).lightness() < 125 ? "white" : "black");
m_colorButton->setStyleSheet("color: " + foreground + "; background-color: " + color);
}
}
调用 myWidget.setEnabled(enabledOption);
禁用小部件,使小部件中的所有文本和所有其他项目都变灰 - 除了这些颜色按钮.
Calling myWidget.setEnabled(enabledOption);
disables the widget, grays out all text and every other items in the widget - except for these color buttons.
所以我正在考虑自己采取行动:
So I am thinking of making my own action:
void MyWidget::setWidgetEnabled(bool enabled)
{
this->setEnabled(enabled);
// what else ?
}
我怎样才能让我的按钮 - 背景颜色和文本颜色我不知道,但按钮确实 - 具有禁用外观"?
How can I make my buttons - of a background color and text color that I don't know, but the button does - have that "disabled look" ?
(注意 - 颜色更新也适用于禁用的项目 - 虽然这并不复杂 - 我在设置小部件禁用时应用的任何样式表都可以在 colorUpdate
函数中应用).
(Note - the color update works on disabled items too - that is not really complicated though - whatever style sheet I apply on setting widget disabled can be applied in the colorUpdate
function).
我只是不知道如何应用带有灰色阴影的样式表 - 或者甚至可能在样式表中有一个禁用"选项......
禁用"中的颜色是什么样的?
I just don't know how to apply a stylesheet with that gray shade - or possibly have a "disabled" option in the stylesheet even...
What do colors look like in "disabled" ?
推荐答案
为禁用状态设置自己的样式.您可以为禁用状态设置特殊的样式 在样式表中:
To set your own style for the disable state. You can set a special style for disabled state in the stylesheet :
m_colorButton->setStyleSheet(":enabled { color: " + foreground
+ "; background-color: " + color
+ " } :disabled { color: " + disabledForeground
+ "; background-color: " + disabledColor + " }");
更改了小部件的代码,而不是全局样式表.
保持默认禁用样式.您只能为启用状态设置自定义样式,然后当禁用小部件时,样式不适用:
To keep the default disabled style. You can set your custom style only for the enabled state, then when the widget is disabled the style does not apply :
m_colorButton->setStyleSheet(":enabled { color: " + foreground
+ "; background-color: " + color + "}");
这篇关于将自定义颜色的按钮设置为禁用样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!