Question

I have created a class that inherits from QSlider. I want to draw a picture on the slider (grabber) instead of showing the plain one. How to do it?

--

I found an answer and posted after I had received the reply. With due respect to the responder, I will choose that reply. However, I'd like to share code so that anyone with the same issue can benefit:

void InheritedSlider::paintEvent(QPaintEvent *event)
{
    // uncomment to draw the parent first. Comment out to just ignore it.
    //QSlider::paintEvent(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    //painter.translate(width() / 2, height() / 2);
    //painter.scale(100 / 200.0, 100 / 200.0);

    QPainterPath volPath;
    volPath.moveTo(60.0, 40.0);
    volPath.arcTo(20.0, 20.0, 40.0, 40.0, 0.0, 360.0);
    volPath.moveTo(40.0, 40.0);
    volPath.lineTo(40.0, 80.0);
    volPath.lineTo(80.0, 80.0);
    volPath.lineTo(80.0, 40.0);
    volPath.closeSubpath();
    painter.drawPath(volPath);
}
Was it helpful?

Solution

You can do it into the paintEvent method of the widget. This allows you to redraw all or only a part of the widget.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top