How to draw a picture instead of a slider on Qt QSlider?

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

-

I found the answer and sent after I received the answer. With due respect to the defendant, I will choose this answer. However, I would like to share the code so that anyone with the same problem can win:

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);
}
+3
source share
1 answer

You can do this in the paintEvent method of the widget. This allows you to redraw all or only part of the widget.

+4
source

Source: https://habr.com/ru/post/1725925/


All Articles