Popover
An anchored popup that avoids screen edges and closes with native popup rules.
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 = "popover";
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* button = new Button(name == "hover-card" ? "@shadcn"
: name == "tooltip" ? "Hover"
: "Open popover",
host);
button->setVariant(Variant::Outline);
layout->addWidget(button);
if (name == "tooltip") {
auto* tip = new Tooltip("Add to library", canvas);
tip->attach(*button);
canvas->setProperty("capturePopup", QVariant::fromValue<QObject*>(tip));
canvas->setProperty("captureAnchor", QVariant::fromValue<QObject*>(button));
} else {
Popover* popup = name == "hover-card" ? static_cast<Popover*>(new HoverCard(canvas))
: new Popover(canvas);
canvas->setProperty("capturePopup", QVariant::fromValue<QObject*>(popup));
canvas->setProperty("captureAnchor", QVariant::fromValue<QObject*>(button));
auto* title = new Label(name == "hover-card" ? "shadcn/ui" : "Dimensions", popup);
popup->content().addWidget(title);
auto* description =
new QLabel(name == "hover-card" ? "Components for building interfaces."
: "Set the dimensions for the layer.",
popup);
description->setWordWrap(true);
popup->content().addWidget(description);
if (name == "hover-card")
static_cast<HoverCard*>(popup)->attach(*button);
else
QObject::connect(button, &Button::clicked, popup,
[popup, button] { popup->showFor(*button); });
}
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::Popover popup(&window);
popup.content().addWidget(new QLabel("Dimensions", &popup));
popup.showFor(anchor, shadcn::Side::Bottom);
The popup repositions when it would cross a screen edge.
API
content() returns the owned popup layout. setContentWidth changes the panel width. actualSide() reports the side used after collision handling.
Keyboard and accessibility
The popup takes focus and closes through Qt popup rules. The anchor should have a clear accessible name.