Skip to main content

Command

A filterable command list that returns application data when an item is activated.

Loading preview…
#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* command = new Command(host);
command->setFixedWidth(340);
command->addGroup("Suggestions");
command->addItem("Calendar");
command->addItem("Search files");
command->addItem("Settings");
layout->addWidget(command);
return canvas;
}

Installation​

add_subdirectory(shadcn-cpp)
target_link_libraries(app PRIVATE shadcn::widgets)

Usage​

#include <shadcn/data.hpp>

shadcn::Command command(parent);
command.setEmptyText("No commands found");
command.addItem("Open settings", "settings", {"preferences"});
QObject::connect(&command, &shadcn::Command::triggered,
receiver, onCommand);

API​

search() and list() return borrowed Qt controls. addItem stores a label, data value and search keywords.

Variations​

Use addGroup to separate command families and a typed QVariant for action data.

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.