Skip to main content

Navigation Menu

A horizontal navigation list with one open menu or page at a time.

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* navigation = new NavigationMenu(host);
(void)navigation->addLink("Home");
(void)navigation->addMenu("Components", "components");
auto* contents = new QWidget(navigation);
auto* links = new QVBoxLayout(contents);
links->addWidget(new Label("Components", contents));
links->addWidget(new QLabel("Buttons, inputs and layouts.", contents));
navigation->setMenuContent("components", *contents);
(void)navigation->addLink("Documentation");
layout->addWidget(navigation);
return canvas;
}

Installation​

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

Usage​

#include <shadcn/navigation.hpp>
#include <QLabel>

shadcn::NavigationMenu menu(parent);
menu.addLink("Home", "home");
menu.addMenu("Products", "products");
menu.setMenuContent(
"products", *new QLabel("Product links", &menu));
menu.setCurrentValue("home");

API​

addLink and addMenu create entries. setMenuContent supplies popup content and currentChanged reports selection.

Variations​

Use links for direct destinations and menus for grouped content.

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.