paint on qslider

  • i subclassed QMainWindow and have a QSlider in the class (as a member), it indicates the level of zoom (something in %). everything works and is fine with it, only i want to paint a line (or i don't know how to call it in english) for the default value that is set, so that when the value of slider changes, that line will be visible. something like this:
    alt text
    i've read that i can subclass QSlider and override its paintEvent() but i don't want to do that. is there any other way?

  • Lifetime Qt Champion

    Subclassing is the way to do it.
    Why dont u want to do that?

    You can still use Designer etc if thats the issue.

    That said, you might be able to do something using event filters but its messy at best.

  • i didn't want to subclass because the paintEvent() is really the only thing "extra" that i'm gonna need from it. but if it's the right way i'll do it

  • Lifetime Qt Champion

    Well i can relate to that as i wanted to draw some extra info on the widgets and did not want to subclass all of them so i tried to do it from outside but since your are not allowed to use QPainter outside paintevent it was not really working.

    So for one widget, sub classing cant be beat, code size wise at least. If you do inline style
    and put in mainwindow.h , its not super involving.

    #include <QSlider>
    #include <QPainter>
    class MySlider : public QSlider {
        explicit MySlider(QWidget *parent = nullptr) : QSlider(parent) {}    
        void paintEvent(QPaintEvent *event) override {
            QPainter p(this);
            p.drawLine(15, 0, 15, 200);

    alt text

  • hi, sorry for the late reply.

    that's exactly what i had done already. i also need have my slider work with jumps. i looked at this answer. actually, the code posted in here works. but it doesn't work at first or second times but rather after i click the slider tick a few times.

  • please help! (read my previous post)

Log in to reply