Qt5 - 将背景颜色设置为 QPushButton 和 QCheckBox [英] Qt5 - setting background color to QPushButton and QCheckBox

查看:120
本文介绍了Qt5 - 将背景颜色设置为 QPushButton 和 QCheckBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改 Qt5 中 QAbstractButton(QPushButton 或 QCheckBox)的背景颜色,但运气为零.

I'm trying to change the background color of a QAbstractButton (either a QPushButton or QCheckBox) in Qt5 and having zero luck.

这什么都不做:

pButton->setAutoFillBackground(true);
QPalette palette = pButton->palette();
palette.setColor(QPalette::Window, QColor(Qt::blue));
pButton->setPalette(palette);
pButton->show();

如果我尝试更改样式表:

and if I try changing the style sheet:

pButton->setStyleSheet("background-color: rgb(255,255,0);");

然后 Qt 举起手来画了一个看起来很恶心的块状按钮.

then Qt throws up its hands and draws an afwul-looking blocky button.

有一个标题为如何更改 QWidget 的背景颜色"但它只是谈论这两种方法.

There is a page titled "How to change the background color of QWidget" but it just talks about those two methods.

还有一个页面Qt 样式表示例" 这意味着如果你想改变背景颜色,你必须接管绘制按钮的所有方面,这似乎有点矫枉过正.

There is also a page "Qt Style Sheets Examples" that implies that if you want to change the background color, you have to take over all aspects of drawing the button, which just seems like overkill.

我需要它在 Mac、Windows 和 Ubuntu Linux 上运行,如果我必须手动绘制关于按钮的所有内容 3 次(每个平台一次),这真的不是一件愉快的事情.

I need this to run on Mac, Windows, and Ubuntu Linux, and it's really not a happy thing if I have to manually draw everything about the button 3 times (once for each platform).

我是否遗漏了一些明显的东西?

Am I missing something obvious?

附言背景颜色"是指按钮周围的区域,而不是按钮表面文字下方的颜色.

p.s. By "background color" I mean the area surrounding the button, not the color under the text on the face of the button.

推荐答案

我遇到了同样的问题,但最终得到了解决.我正在使用带有 Fusion 颜色主题的 Qt 5:

I had the same issue, but finally got this to work. I'm using Qt 5 with the Fusion color theme:

QPalette pal = button->palette();
pal.setColor(QPalette::Button, QColor(Qt::blue));
button->setAutoFillBackground(true);
button->setPalette(pal);
button->update();

按照上面的确切顺序尝试这些命令,如果仍然不起作用,请将您的主题设置为 Fusion 并重试.

Try these commands in the exact order as above, and if that still doesn't work, set your theme to Fusion and try again.

祝你好运!

这篇关于Qt5 - 将背景颜色设置为 QPushButton 和 QCheckBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆