user3420412
user3420412

Reputation: 41

How to put ticks at custom places on a Qslider?

Hi I tried to implement a custom QSlider, but the ticks are always in intervalls, and i need to put them at specific places. I have no idea on how to proceed.

Upvotes: 4

Views: 4391

Answers (1)

Rick Smith
Rick Smith

Reputation: 9251

If you are just want to change the tick mark intervals, you can use QSlider:setTickInterval().

From the documentation:

tickInterval : int

This property holds the interval between tickmarks.

This is a value interval, not a pixel interval. If it is 0, the slider will choose between singleStep() and pageStep(). The default value is 0.

If you want marks at non-regular intervals, you are going to need to override paint() (see example).

This is some untested example code:

void MyWidget::paintEvent(QPaintEvent* event)
{
    QSlider::paintEvent(event);  // paints the slider like normal

    // Add your custom tick locations
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::darkGreen);
    painter.drawRect(1, 2, 6, 4);    

    painter.setPen(Qt::darkGray);
    painter.drawLine(2, 8, 6, 2);
}

This probably contains a few errors, but it should illustrate the idea nicely. You can also see this question. Good luck!

Upvotes: 5

Related Questions