Qt slider widget with tick text labels

I would use a QSlider with QLabel(s) attached as below. My usual disclaimer goes that you may need some fine-tuning and adjustment on your own for sure.

main.cpp

#include <QMainWindow>
#include <QApplication>
#include <QGridLayout>
#include <QSlider>
#include <QLabel>

class MainWindow Q_DECL_FINAL : public QMainWindow
{
    Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = Q_NULLPTR) : QMainWindow(parent)
        {
            QSlider *slider = new QSlider(Qt::Horizontal, this);
            slider->setRange(1, 4);
            slider->setSingleStep(1);
            QLabel *label1 = new QLabel("Novice", this);
            QLabel *label2 = new QLabel("Intermediate", this);
            QLabel *label3 = new QLabel("Advanced", this);
            QLabel *label4 = new QLabel("Expert", this);
            QGridLayout *layout = new QGridLayout;
            layout->addWidget(slider, 0, 0, 1, 4);
            layout->addWidget(label1, 1, 0, 1, 1);
            layout->addWidget(label2, 1, 1, 1, 1);
            layout->addWidget(label3, 1, 2, 1, 1);
            layout->addWidget(label4, 1, 3, 1, 1);
            setLayout(layout);
        }
};

#include "main.moc"

int main(int argc, char **argv)
{
    QApplication application(argc, argv);
    MainWindow mainWindow;
    mainWindow.show();
    return application.exec();
}

main.pro

TEMPLATE = app
TARGET = main
QT += widgets
SOURCES += main.cpp

Build and Run

qmake && make && ./main

You can use my extended QSlider class in git.


I had a similar need, I ended up solving it by overriding the paintEvent. Here's a PyQt solution:


class Slider(QSlider):
    """Slider with labels (instead of tick marks) along each available position"""

    def __init__(self, parent=None):
        super(Slider, self).__init__(parent)
        # this decides the range and the labels placed along the slider
        self.values = ['one', 'two', 'three', 'four', 'five']
        self.setOrientation(Qt.Horizontal)
        self.setTickInterval(1)
        self.setTickPosition(QSlider.NoTicks)
        self.setSingleStep(1)
        self.setRange(0, len(self.values) - 1)
        self.setValue(0)

    def paintEvent(self, event):
        QSlider.paintEvent(self, event)

        rect = self.geometry()
        curr_value = str(self.value() / 1000.00)
        round_value = round(float(curr_value), 2)
        # start painter
        painter = QPainter(self)
        painter.setPen(QPen(Qt.black))

        font_metrics = QFontMetrics(self.font())
        font_width = font_metrics.boundingRect(str(round_value)).width()

        for index, value in enumerate(self.values):
            print(index, value)
            # first and last position need a slightly different offset so the text doesn't get cut
            if index == 0:
                # first position
                pos = QStyle.sliderPositionFromValue(self.minimum(), self.maximum(), index, self.width())
                painter.drawText(QPoint(pos, rect.height()), value)
            elif index == len(self.values) - 1:
                # last position
                pos = QStyle.sliderPositionFromValue(self.minimum(), self.maximum(), index, self.width())
                painter.drawText(QPoint(pos - font_width, rect.height()), value)
            else:
                pos = QStyle.sliderPositionFromValue(self.minimum(), self.maximum(), index, self.width())
                painter.drawText(QPoint(pos - font_width / 2.0, rect.height()), value)

Ends up looking something like this:

enter image description here

Tags:

C++

Qt

Qtgui