Qt5 QSlider调色板在windows下没有效果 [英] Qt5 QSlider color palette no effect under windows

查看:54
本文介绍了Qt5 QSlider调色板在windows下没有效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用

slider->setPalette(QPalette(mycolor, mycolor));

为滑块着色(Qt5).在 Linux 下这可以完美运行,但在 Windows 7 下,滑块看起来完全正常,没有任何颜色变化.

有没有办法让windows下有彩色的滑块?

解决方案

您需要在 QSlider 上调用 setAutoFillBackground(true) 才能使其工作:

QPalette p;p.setColor(QPalette::Window, QColor(42, 42, 42));滑块-> setAutoFillBackground(true);滑块-> setPalette(p);

作为替代,您可以使用QSS:

slider->setStyleSheet("QSlider::groove:horizo​​ntal { background-color: red; } ");

用于水平滑块或

slider->setStyleSheet("QSlider::groove:vertical{ background-color: red; } ");

用于垂直滑块.

如果你想设置一个 QColor 你会想要做这样的事情:

QColor myColor;myColor.setRgb(42, 42, 42);QString backgroundColor = QString("rgb("%1, %2, %3);").arg(myColor.red()).arg(myColor.green()).arg(myColor.blue());");滑块-> setStyleSheet("QSlider::groove:vertical{ background-color: " + backgroundColor + " } ");

所以结果会是这样的:

QSlider::groove:vertical{ background-color: rgb(42, 42, 42);}

更多详情此处.>

I use

slider->setPalette(QPalette(mycolor, mycolor));

to color the slider (Qt5). Under Linux this works perfectly but under Windows 7, the sliders just look completely normal without any change in color.

Is there any way to get colored sliders under windows?

解决方案

You need to call setAutoFillBackground(true) on the QSlider in order for this to work:

QPalette p;
p.setColor(QPalette::Window, QColor(42, 42, 42));
slider->setAutoFillBackground(true);
slider->setPalette(p);

As an alternative, you can use QSS:

slider->setStyleSheet("QSlider::groove:horizontal { background-color: red; } ");

for horizontal sliders or

slider->setStyleSheet("QSlider::groove:vertical{ background-color: red; } ");

for vertical sliders.

If you want to set a QColor you would want to do something like:

QColor myColor;
myColor.setRgb(42, 42, 42);
QString backgroundColor = QString("rgb("%1, %2, %3);").arg(myColor.red()).arg(myColor.green()).arg(myColor.blue());");
slider->setStyleSheet("QSlider::groove:vertical{ background-color: " + backgroundColor  + " } ");

so that the result would be something like:

QSlider::groove:vertical{ background-color: rgb(42, 42, 42); }

More details here.

这篇关于Qt5 QSlider调色板在windows下没有效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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