带刻度文本标签的 Qt 滑块小部件

问题描述 投票:0回答:4

我正在寻找像 QSlider 这样的 Qt 小部件,但具有文本刻度标签支持,如下所示:example widget

这个小部件我将用作模式切换器。你遇到过这样的事吗

c++ qt qtgui
4个回答
20
投票

我会使用带有

QSlider
QLabel
,如下所示。我通常的免责声明是,您可能确实需要自己进行一些微调和调整。

主.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

构建并运行

qmake && make && ./main

3
投票

您可以在 git 中使用我的扩展 QSlider 类。


2
投票

我有类似的需求,我最终通过覆盖paintEvent来解决它。这是一个 PyQt 解决方案:


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)

最终看起来像这样:

enter image description here


0
投票

由于某种原因,最后一个选项无法正常工作,开关仅切换到第一个选项和最后一个选项((

© www.soinside.com 2019 - 2024. All rights reserved.