Badge
A compact text label for status, category and short context.
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* row = new QHBoxLayout;
row->setSpacing(8);
const std::array variants{Variant::Default, Variant::Secondary, Variant::Outline,
Variant::Destructive};
const std::array labels{"Badge", "Secondary", "Outline", "Destructive"};
for (std::size_t i = 0; i < variants.size(); ++i) {
auto* b = new Badge(labels[i], host);
b->setVariant(variants[i]);
row->addWidget(b);
}
layout->addLayout(row);
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/widgets.hpp>
shadcn::Badge badge("In progress", parent);
badge.setVariant(shadcn::Variant::Secondary);
API
setVariant changes the shared appearance. The text and inherited QLabel behavior remain available.
Keyboard and accessibility
Badge is static text. Give it a meaningful accessible name when the visible text is abbreviated or the badge carries information that is missing from nearby text.