Reputation: 41
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
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