Dialog
A modal surface with a backdrop, focus containment and focus restoration.
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;
const QString name = "dialog";
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();
Dialog* dialog = name == "alert-dialog" ? static_cast<Dialog*>(new AlertDialog(canvas))
: name == "sheet" ? static_cast<Dialog*>(new Sheet(Side::Right, canvas))
: name == "drawer" ? static_cast<Dialog*>(new Drawer(canvas))
: new Dialog(canvas);
dialog->setTitle(name == "alert-dialog" ? "Are you sure?" : "Edit profile");
dialog->setDescription(name == "alert-dialog"
? "This action cannot be undone."
: "Make changes to your profile here. Save when you are done.");
if (name != "alert-dialog") {
auto* label = new Label("Name", &dialog->panel());
auto* input = new Input(&dialog->panel());
input->setText("Alex Morgan");
label->setBuddy(input);
dialog->content().addWidget(label);
dialog->content().addWidget(input);
auto* save = new Button("Save changes", &dialog->panel());
dialog->footer().addWidget(save);
QObject::connect(save, &Button::clicked, dialog, &Dialog::accept);
}
auto* button = new Button(name == "alert-dialog" ? "Delete account" : "Edit profile", host);
button->setVariant(Variant::Outline);
layout->addWidget(button);
QObject::connect(button, &Button::clicked, dialog, &Dialog::open);
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/overlays.hpp>
shadcn::Dialog dialog(&window);
dialog.setTitle("Edit profile");
dialog.setDescription("Make changes to your profile here.");
dialog.content().addWidget(new shadcn::Input(&dialog.panel()));
dialog.open();
Add footer actions through footer(). Call accept() or reject() from a button signal.
API
content(), footer() and panel() return borrowed references to objects owned by the dialog. setContentWidth changes the panel width. Outside clicks dismiss the dialog by default.
Keyboard and accessibility
The dialog restores the previously focused widget after closing. Set a title and description before opening it. Keep the first useful action in the footer.