I am unsuccessfully trying to change the color of the handle on a QSlider. How do I change it to a custom color? I have subclassed QSlider so that I can reimplement the paint event and draw my own rectangle on it--now I want to set the color of the handle. In the creator of the subclassed widget, I did this:
setStyleSheet("handle:horizontal {color: red}");
I have tried all kinds of other properties besides color, including background-color, etc--nothing makes any change at all on the widget--it still stays the default grey. I'd swear my code wasn't executing at all, but I checked it in the debugger, and it is.
I tried moving the setStyleSheet into my paintEvent,; that made no difference. Here is the paintEvent--I don't know if it matters, but I suspect I'll get asked for it if I don't include it
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));
}