Qt 如何更改QSlider上手柄的颜色
我尝试更改QSlider上手柄的颜色失败。如何将其更改为自定义颜色?我已经对QSlider进行了子类化,以便可以重新实现paint事件并在其上绘制自己的矩形——现在我想设置句柄的颜色。在子类小部件的创建者中,我做了以下操作:Qt 如何更改QSlider上手柄的颜色,qt,qslider,qstylesheet,Qt,Qslider,Qstylesheet,我尝试更改QSlider上手柄的颜色失败。如何将其更改为自定义颜色?我已经对QSlider进行了子类化,以便可以重新实现paint事件并在其上绘制自己的矩形——现在我想设置句柄的颜色。在子类小部件的创建者中,我做了以下操作: setStyleSheet("handle:horizontal {color: red}"); 除了颜色之外,我还尝试了其他各种属性,包括背景色,等等——小部件上没有任何变化——它仍然保持默认的灰色。我发誓我的代码根本没有执行,但我在调试器中检查了它,确实如此 我试着将
setStyleSheet("handle:horizontal {color: red}");
除了颜色之外,我还尝试了其他各种属性,包括背景色,等等——小部件上没有任何变化——它仍然保持默认的灰色。我发誓我的代码根本没有执行,但我在调试器中检查了它,确实如此
我试着将setStyleSheet移动到我的paintEvent中,;这没什么区别。这是paintEvent——我不知道它是否重要,但我怀疑如果我不包括它,会有人问我
void FilledSlider::paintEvent(QPaintEvent *ev) {
QStyleOptionSlider opt;
initStyleOption(&opt);
opt.subControls = QStyle::SC_SliderGroove | QStyle::SC_SliderHandle;
if (tickPosition() != NoTicks)
{
opt.subControls |= QStyle::SC_SliderTickmarks;
}
QRect groove_rect = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
QSlider::paintEvent(ev);
int grooveCenter = groove_rect.bottom() - (groove_rect.bottom() - groove_rect.top())/2.0;
QRect fillRectangle;
int left;
int width;
if(direction < 0)
{
left = groove_rect.left() + ((1.0 -filledPercentage) * groove_rect.width());
width = filledPercentage * groove_rect.width();
}
else
{
left = groove_rect.left();
width = groove_rect.width() * filledPercentage;
}
fillRectangle.setRect(left, grooveCenter, width, 0.2*groove_rect.height());
QPainter painter(this);
painter.fillRect(fillRectangle, QBrush(Qt::red));
}
void FilledSlider::paintEvent(QPaintEvent*ev){
QStyleOptionSlider opt;
initStyleOption(&opt);
opt.subcrols=QStyle::SC|u SliderGroove | QStyle::SC|u SliderHandle;
if(tickPosition()!=NoTicks)
{
opt.subcrols |=QStyle::SC|U滑块标记;
}
QRect groove_rect=style()->subcrolrect(QStyle::CC_Slider,&opt,QStyle::SC_SliderGroove,this);
QSlider::paintEvent(ev);
int grooveCenter=groove_rect.bottom()-(groove_rect.bottom()-groove_rect.top())/2.0;
矩形;
int左;
整数宽度;
如果(方向<0)
{
左=凹槽左()角+((1.0-填充百分比)*凹槽右宽度();
宽度=填充百分比*槽的矩形宽度();
}
其他的
{
左=槽右左();
宽度=槽直径宽度()*填充百分比;
}
fillRectangle.setRect(左,grooveCenter,宽度,0.2*groove_rect.height());
油漆工(本);
fillRect(fillRectangle,QBrush(Qt::red));
}
对应于这个try-setStyleSheet(“QSlider::handle:horizontal{background color:red;}”)
这就是答案,谢谢你 你从哪里学来的绘画技巧?对你使用的参考资料感到好奇。它可能会帮助我找到答案代码>。如果它不起作用,请使用casual
QSlider
,而不是subclass。您可能会损坏paintEvent
或其他内容。我从这里复制了paint事件:我尝试将对象从子类版本更改为普通QSlider——应用了样式,但没有任何区别