Card
A bordered surface with owned header, content, action and footer layouts.
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* card = new Card(host);
card->setTitle("Create project");
card->setDescription("Start a new project in one click.");
card->setFixedWidth(350);
auto* label = new Label("Name", card);
auto* input = new Input(card);
input->setPlaceholderText("Project name");
label->setBuddy(input);
card->content().addWidget(label);
card->content().addWidget(input);
auto* cancel = new Button("Cancel", card);
cancel->setVariant(Variant::Outline);
card->footer().addWidget(cancel);
card->footer().addStretch();
card->footer().addWidget(new Button("Create", card));
layout->addWidget(card);
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::Card card(parent);
card.setTitle("Modern C++");
card.setDescription("Continue the current lesson.");
card.content().addWidget(new shadcn::Button("Continue", &card));
For parent owned construction, use make_child.
auto& card = shadcn::make_child<shadcn::Card>(window);
card.setTitle("Modern C++");
card.content().addWidget(
shadcn::make_child<shadcn::Button>(card, "Continue"));
layout.addWidget(&card);
API
content(), footer() and action() return borrowed references to layouts owned by the card. Add child widgets through those layouts.
Ownership
The card owns its child widgets through Qt parent ownership. Do not delete a child returned by make_child or keep a reference after its parent is destroyed.