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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With