VR-Engage  2.2
Loading...
Searching...
No Matches
makVre::DtChatQmlModel Class Reference

Detailed Description

This class demonstrates the REUSABLE pattern for creating custom Qt models that expose C++ data to QML user interfaces. Key concepts:

  • Inheriting from QAbstractListModel for list-based data
  • Defining custom roles for QML property access
  • Implementing required virtual methods (rowCount, data, roleNames)
  • Proper model change notifications (beginInsertRows, endInsertRows, etc.)

This pattern can be adapted for any list-based data that needs to be displayed in a QML ListView or Repeater.

#include <exampleQtQuick.h>

Inheritance diagram for makVre::DtChatQmlModel:
[legend]

Public Types

enum  ChatRoles { SenderRole = Qt::UserRole + 1 , ReceiverRole , TimeRole , MessageRole }
 

Public Member Functions

 DtChatQmlModel (QObject *parent=nullptr)
 
virtual void addChatEntry (const DtChatEntry &entry)
 
virtual void clear ()
 
virtual int getIndexOfName (const QString &messageName)
 
virtual QString getMessage (const int idx)
 
virtual bool removeRows (int row, int column=0, const QModelIndex &parent=QModelIndex()) override
 
virtual int rowCount (const QModelIndex &parent=QModelIndex()) const override
 
virtual QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const override
 
virtual bool setData (const QModelIndex &index, const QVariant &value, int role) override
 

Protected Member Functions

virtual QHash< int, QByteArray > roleNames () const override
 

Protected Attributes

QList< DtChatEntrymyChatEntries
 

Member Enumeration Documentation

◆ ChatRoles

Custom roles for accessing chat entry fields from QML.

Qt roles allow QML to access C++ data using named properties. Start at Qt::UserRole + 1 to avoid conflicts with standard roles.

Enumerator
SenderRole 

Access sender name via "sender" in QML.

ReceiverRole 

Access receiver name via "receiver" in QML.

TimeRole 

Access message time via "time" in QML.

MessageRole 

Access message text via "msg" in QML.

Constructor & Destructor Documentation

◆ DtChatQmlModel()

makVre::DtChatQmlModel::DtChatQmlModel ( QObject * parent = nullptr)

Constructor initializing the model.

Parameters
parentOptional parent QObject for Qt ownership hierarchy

Member Function Documentation

◆ addChatEntry()

virtual void makVre::DtChatQmlModel::addChatEntry ( const DtChatEntry & entry)
virtual

Adds a new chat entry to the model.

This demonstrates proper model update notifications:

  • beginInsertRows notifies views that data is being added
  • Actual data modification
  • endInsertRows triggers view updates
Parameters
entryThe chat entry to add to the model

◆ clear()

virtual void makVre::DtChatQmlModel::clear ( )
virtual

Clears all chat entries from the model.

Uses beginResetModel/endResetModel for bulk changes.

◆ getIndexOfName()

virtual int makVre::DtChatQmlModel::getIndexOfName ( const QString & messageName)
virtual

Finds the index of a message by its text content.

Parameters
messageNameThe message text to search for
Returns
Index of the message, or -1 if not found

◆ getMessage()

virtual QString makVre::DtChatQmlModel::getMessage ( const int idx)
virtual

Gets the message text at a specific index.

Parameters
idxThe index of the message
Returns
The message text

◆ removeRows()

virtual bool makVre::DtChatQmlModel::removeRows ( int row,
int column = 0,
const QModelIndex & parent = QModelIndex() )
overridevirtual

Removes rows from the model.

Parameters
rowThe row index to remove
columnUnused for list models (default 0)
parentUnused for list models
Returns
true if removal succeeded

◆ rowCount()

virtual int makVre::DtChatQmlModel::rowCount ( const QModelIndex & parent = QModelIndex()) const
overridevirtual

Returns the number of entries in the model.

Parameters
parentUnused for list models
Returns
Number of chat entries

◆ data()

virtual QVariant makVre::DtChatQmlModel::data ( const QModelIndex & index,
int role = Qt::DisplayRole ) const
overridevirtual

Returns data for a specific role at a given index.

This is the core method QML uses to access data.

Parameters
indexThe model index of the item
roleThe data role being requested (SenderRole, MessageRole, etc.)
Returns
The requested data as a QVariant

◆ setData()

virtual bool makVre::DtChatQmlModel::setData ( const QModelIndex & index,
const QVariant & value,
int role )
overridevirtual

Sets data for a specific role at a given index.

Parameters
indexThe model index of the item
valueThe new value to set
roleThe data role being modified
Returns
true if data was successfully set

◆ roleNames()

virtual QHash< int, QByteArray > makVre::DtChatQmlModel::roleNames ( ) const
overrideprotectedvirtual

Maps role enum values to QML property names.

This method tells Qt how to expose roles as named properties in QML. For example, SenderRole becomes accessible as "sender" in QML.

Returns
Hash mapping role integers to QML property names

Member Data Documentation

◆ myChatEntries

QList<DtChatEntry> makVre::DtChatQmlModel::myChatEntries
protected

Storage for all chat entries.


The documentation for this class was generated from the following file: