Scroll Area
A QScrollArea with shared scrollbars, focus styling and visibility controls.
Loading preview…
Live C++Source and licences
#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* scroll = new ScrollArea(host);
scroll->setFixedSize(280, 210);
auto* contents = new QWidget;
auto* rows = new QVBoxLayout(contents);
rows->addWidget(new Label("Tags", contents));
for (int i = 1; i <= 30; ++i) {
rows->addWidget(new QLabel(QString("v1.2.0-beta.%1").arg(i), contents));
rows->addWidget(new Separator(Qt::Horizontal, contents));
}
scroll->setWidget(contents);
layout->addWidget(scroll);
return canvas;
}
Installation
- Source
- Installed package
add_subdirectory(shadcn-cpp)
target_link_libraries(app PRIVATE shadcn::widgets)
find_package(shadcn CONFIG REQUIRED)
target_link_libraries(app PRIVATE shadcn::widgets)
Usage
#include <shadcn/navigation.hpp>
#include <QLabel>
shadcn::ScrollArea area(parent);
area.setWidgetResizable(true);
area.setWidget(new QLabel("Scrollable content", &area));
area.setHorizontalScrollBarVisible(false);
API
ScrollArea inherits QScrollArea. Use the two visibility setters for horizontal and vertical bars.
Variations
Keep horizontal scrolling for tables or code and disable it for ordinary reading content.
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.