Skip to main content

Message

A chat message row with optional avatar, header, footer and content slots.

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* message = new Message(host);
message->setFixedWidth(380);
auto* avatar = new Avatar("AM", message);
message->setAvatar(*avatar);
message->addHeader(*new Label("Alex Morgan", message));
message->setText("The design looks good. Let's build it.");
message->addFooter(*new QLabel("10:42 AM", message));
layout->addWidget(message);
return canvas;
}

Installation​

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

Usage​

#include <shadcn/feedback.hpp>

shadcn::Message message(parent);
message.setText("Your export is ready.");
message.setAlign(shadcn::MessageAlign::Start);
auto& avatar = shadcn::make_child<shadcn::Avatar>(message, "AM");
message.setAvatar(avatar);

API​

setText, setAlign, setAvatar, addHeader, addContent and addFooter compose the row.

Variations​

Use end alignment for the local speaker and add header or footer widgets for metadata.

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.