如何设置组合框或双旋转框等小部件的背景颜色?

发布于 2024-07-07 00:58:29 字数 223 浏览 6 评论 0原文

我正在尝试设置双旋转框的背景颜色,但我不确定应该使用什么功能。

我看到一些名为 SetBackgroundRole 的函数,它接受 Qt::ColorRole,但我也不知道如何使用这个函数。

请告诉我,更改 QComboBoxQDoubleSpinBox 背景颜色的简单方法是什么?

I am trying to set the background color for a double spin box, and I am not sure what function I should use.

I saw some function called SetBackgroundRole which accepts a Qt::ColorRole, but I am not sure how to use this one as well.

Kindly let me know, what's the simple way to change the background color of a QComboBox or QDoubleSpinBox?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

筑梦 2024-07-14 00:58:30
comboBox->setPalette( QPalette( Qt::blue ) );

对我来说效果很好!

comboBox->setPalette( QPalette( Qt::blue ) );

Works fine for me!

丑丑阿 2024-07-14 00:58:30

我会尝试类似的东西

QPalette pal = widget.palette();
pal.setColor(QPalette::Window, Qt::blue);
widget.setPalette(pal);

I'd try something like

QPalette pal = widget.palette();
pal.setColor(QPalette::Window, Qt::blue);
widget.setPalette(pal);
孤芳又自赏 2024-07-14 00:58:30

以前的答案对我来说没有用,但我混合了所有答案,最终在 Qt 5.12 上工作:

QPalette pal = ui.widget->palette();
pal.setColor(QPalette::Base, Qt::red);
ui.widget->setPalette(pal);

No previously answers worked for me, but I made a mix of all responses and finally worked on Qt 5.12:

QPalette pal = ui.widget->palette();
pal.setColor(QPalette::Base, Qt::red);
ui.widget->setPalette(pal);
诗化ㄋ丶相逢 2024-07-14 00:58:30

实际上,如果您在 QComboBox 的情况下查看 QPalette 的 Qt 文档,后台角色可能不是您想要的。 你想要的是:

QPalette::Base 主要用作文本输入小部件的背景颜色,但也可用于其他绘画 - 例如组合框下拉列表和工具栏手柄的背景。 它通常是白色或其他浅色。

所以这里是我用来设置组合框背景颜色的代码,我用来匹配它所在小部件的颜色:

QPalette pal = myComboBox->palette();
pal.setColor(QPalette::Base, pal.color(QPalette::Window));
myComboBox->setPalette(pal);

Actually, if you look at the Qt docs for QPalette in the case of a QComboBox the background role is probably not what you want. What you want is:

QPalette::Base Used mostly as the background color for text entry widgets, but can also be used for other painting - such as the background of combobox drop down lists and toolbar handles. It is usually white or another light color.

So here is the code I am using to set the background color of a combo box I am using to match the color of the widget it is on:

QPalette pal = myComboBox->palette();
pal.setColor(QPalette::Base, pal.color(QPalette::Window));
myComboBox->setPalette(pal);
七秒鱼° 2024-07-14 00:58:30

显然,在 Qt 4.1 及更高版本中,您需要设置 this->setAutoFillBackground( true ); 以使调色板应用背景颜色。

Apparently in Qt 4.1 and onwards, you need to set this->setAutoFillBackground( true ); for the palette to apply the background color.

谜兔 2024-07-14 00:58:30

虽然前面的答案可能会为不可编辑的 QComboBox 设置背景颜色,但它们不适用于可编辑的 QComboBox。 对于这种情况,您需要派生用于编辑的 QLineEdit 小部件并重置其背景。

这是我让它工作的方法:

    QComboBox *myComboBox = new QComboBox();
    myComboBox->setEditable(true);
    QColor backColor = QColor(246, 230, 230);
    QLineEdit *lineEditor = myComboBox->lineEdit();
    QPalette pal = lineEditor->palette();
    pal.setColor(QPalette::Base, backColor);
    lineEditor->setPalette(pal);

While the previous answers may set the background color for a non-editable QComboBox, they do not work for an editable QComboBox. For that case, you need to derive the QLineEdit widget used for the editing and reset its background.

Here is how I got it to work:

    QComboBox *myComboBox = new QComboBox();
    myComboBox->setEditable(true);
    QColor backColor = QColor(246, 230, 230);
    QLineEdit *lineEditor = myComboBox->lineEdit();
    QPalette pal = lineEditor->palette();
    pal.setColor(QPalette::Base, backColor);
    lineEditor->setPalette(pal);
粉红×色少女 2024-07-14 00:58:30

构建一个蓝色的调色板,无论实际的小部件是什么:

comboBox->setPalette( QPalette( Qt::blue ) );

Construct a palette that is blue no matter what the actual widget:

comboBox->setPalette( QPalette( Qt::blue ) );
随梦而飞# 2024-07-14 00:58:30

fhe 通常是正确的,但没有考虑在调色板中使用不同背景角色的小部件(如旋转框和按钮/组合框)。 一种更通用的解决方案是这样的:

QPalette pal = widget.palette();
pal.setColor(widget.backgroundRole(), Qt::blue);
widget.setPalette(pal);

或者,您可以查看各种调色板角色的描述并找出您想要的角色,然后将其应用到包含您想要更改的其他角色的小部件。 调色板更改应该传播到子窗口小部件。

fhe is generally correct, but doesn't account for the widgets (like spin boxes and buttons/comboboxes) that use a different background role in the palette. A more general solution would be something like this:

QPalette pal = widget.palette();
pal.setColor(widget.backgroundRole(), Qt::blue);
widget.setPalette(pal);

Alternatively, you could look into the descriptions of the various palette roles and figure out the one you want, then apply it to the widget containing the others you want changed. The palette changes should propagate to the children widgets.

明月松间行 2024-07-14 00:58:30

使用 QPalette 并不保证适用于所有样式,因为样式作者受到不同平台指南和本机主题引擎的限制。

为了确保您的背景颜色正确,我建议使用 Qt 样式表。 这是我更改 QComboBox 的背景颜色所做的操作:

myComboBox->setStyleSheet("QComboBox { background-color: blue; }");

我还没有'我专门尝试过 QSpinBox,但我想它的工作原理是一样的!

Using a QPalette isn't guaranteed to work for all styles, because style authors are restricted by the different platforms' guidelines and by the native theme engine.

To make sure your background color will be correct, I would suggest to use the Qt Style Sheet. Here is what I did to change the background color of a QComboBox:

myComboBox->setStyleSheet("QComboBox { background-color: blue; }");

I haven't specifically tried for a QSpinBox, but I guess it'll work the same !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文