Skip to main content

Slider

A keyboard and pointer controlled single value or range slider.

Loading preview…
#include <QApplication>
#include <QFile>
#include <QHBoxLayout>
#include <QLabel>
#include <QPainter>
#include <QStandardItemModel>
#include <QUrl>
#include <QVBoxLayout>
#include <array>
#ifdef SHADCN_GALLERY_MEDIA
#include <shadcn/media.hpp>
#endif
#include <shadcn/shadcn.hpp>

QWidget* example(QWidget* parent = nullptr) {
using namespace shadcn;
auto* canvas = new QWidget(parent);
canvas->setAutoFillBackground(true);
auto* outer = new QHBoxLayout(canvas);
outer->setContentsMargins(32, 32, 32, 32);
auto* host = new QWidget(canvas);
auto* layout = new QVBoxLayout(host);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(16);
outer->addStretch();
outer->addWidget(host, 0, Qt::AlignCenter);
outer->addStretch();
auto* slider = new Slider(host);
slider->setFixedWidth(320);
slider->setValues({25, 75});
slider->setAccessibleName("Price range");
layout->addWidget(slider);
return canvas;
}

Installation​

add_subdirectory(shadcn-cpp)
target_link_libraries(app PRIVATE shadcn::widgets)

Usage​

#include <shadcn/controls.hpp>

shadcn::Slider slider(0, 100, parent);
slider.setValues({20, 80});
slider.setSingleStep(5);
QObject::connect(&slider, &shadcn::Slider::valuesChanged,
receiver, onValuesChanged);

API​

setRange sets the bounds and setValues accepts one or more thumbs. valuesChanged reports current values. Dragging a thumb past its neighbours pushes them in the same direction. Keyboard and accessibility value changes stop at the adjacent thumb, matching the stock Base UI behavior.

Variations​

Pass one value for a single thumb and set vertical orientation for a column layout.

Keyboard and accessibility​

Qt supplies native focus, keyboard interaction and accessible object roles. Give the component a visible label or an accessible name when its text alone does not explain the action.

Ownership​

The parent widget owns the component. Child widgets created through a component layout follow Qt parent ownership. References returned by helper methods are borrowed.