• ChatMessageModel
  • ChatMessageModel Class

    Exposes a list of chat messages in a given channel. More...

    Header: #include <ChatMessageModel>
    Inherits: QAbstractListModel

    Public Functions

    ChatMessageModel(QObject *parent = nullptr)
    void appendMessage(const ChatMessagePayload &msg)
    void clear()
    const QList<ChatMessagePayload> &messages() const

    Reimplemented Public Functions

    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
    virtual QHash<int, QByteArray> roleNames() const override
    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override

    Detailed Description

    Overview

    ChatMessageModel is a QAbstractListModel that holds a collection of ChatMessagePayload objects. It provides role-based access to properties like message ID, sender, channel, and authored time. This model is typically used in combination with ChannelMessageProxyModel to display only messages relevant to a specific channel.

    Messages can be appended from either C++ or QML, and the model supports full clearing and retrieval of the raw backing list.

    Internal Members

    Private Variables

    • m_messages (QList<ChatMessagePayload>) – Internal list of all messages stored in reverse-chronological order.

    See also ChannelMessageProxyModel and ChatMessagePayload.

    Member Function Documentation

    [explicit] ChatMessageModel::ChatMessageModel(QObject *parent = nullptr)

    Constructs an empty ChatMessageModel instance.

    parent is the optional QObject parent.

    [invokable] void ChatMessageModel::appendMessage(const ChatMessagePayload &msg)

    Appends a new chat message msg to the top of the model.

    This inserts the message in reverse-chronological order (most recent first).

    Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

    void ChatMessageModel::clear()

    Removes all messages from the model.

    This is typically used during disconnection or channel switching.

    [override virtual] QVariant ChatMessageModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const

    Reimplements: QAbstractItemModel::data(const QModelIndex &index, int role) const.

    Returns the data for the message at index for a given role.

    This includes properties such as ID, username, channel name, and message body. Returns an invalid QVariant if the index is out of range.

    const QList<ChatMessagePayload> &ChatMessageModel::messages() const

    Returns a const reference to the internal message list.

    This allows controller classes to inspect or iterate over the full model contents.

    [override virtual] QHash<int, QByteArray> ChatMessageModel::roleNames() const

    Reimplements: QAbstractItemModel::roleNames() const.

    Returns a mapping from role values to their corresponding QML role names.

    This is used by QML delegates to access message properties directly in views.

    [override virtual] int ChatMessageModel::rowCount(const QModelIndex &parent = QModelIndex()) const

    Reimplements: QAbstractItemModel::rowCount(const QModelIndex &parent) const.

    Returns the number of messages in the model.

    This is used by QML views to determine how many items to render. parent is unused for flat list models.