开发者

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

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.line开发者_如何学GoTo(80.0, 80.0);
    volPath.lineTo(80.0, 40.0);
    volPath.closeSubpath();
    painter.drawPath(volPath);
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜