|
VR-Engage
2.2
|
This class demonstrates the REUSABLE pattern for creating custom Qt models that expose C++ data to QML user interfaces. Key concepts:
This pattern can be adapted for any list-based data that needs to be displayed in a QML ListView or Repeater.
#include <exampleQtQuick.h>
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< DtChatEntry > | myChatEntries |
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.
| makVre::DtChatQmlModel::DtChatQmlModel | ( | QObject * | parent = nullptr | ) |
Constructor initializing the model.
| parent | Optional parent QObject for Qt ownership hierarchy |
|
virtual |
Adds a new chat entry to the model.
This demonstrates proper model update notifications:
| entry | The chat entry to add to the model |
|
virtual |
Clears all chat entries from the model.
Uses beginResetModel/endResetModel for bulk changes.
|
virtual |
Finds the index of a message by its text content.
| messageName | The message text to search for |
|
virtual |
Gets the message text at a specific index.
| idx | The index of the message |
|
overridevirtual |
Removes rows from the model.
| row | The row index to remove |
| column | Unused for list models (default 0) |
| parent | Unused for list models |
|
overridevirtual |
Returns the number of entries in the model.
| parent | Unused for list models |
|
overridevirtual |
Returns data for a specific role at a given index.
This is the core method QML uses to access data.
| index | The model index of the item |
| role | The data role being requested (SenderRole, MessageRole, etc.) |
|
overridevirtual |
Sets data for a specific role at a given index.
| index | The model index of the item |
| value | The new value to set |
| role | The data role being modified |
|
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.
|
protected |
Storage for all chat entries.