QtDataSync
4.2.0
A simple offline-first synchronisation framework, to synchronize data of Qt applications between devices
|
A passive item model for a datasync data store. More...
#include <datastoremodel.h>
Public Slots | |
void | setTypeId (int typeId) |
WRITE accessor for DataStoreModel::typeId. | |
void | setTypeId (int typeId, bool resetColumns) |
WRITE accessor for DataStoreModel::typeId. | |
void | setEditable (bool editable) |
WRITE accessor for DataStoreModel::editable. | |
void | reload () |
Reloads all data in the model. | |
Signals | |
void | storeError (const QException &exception, QPrivateSignal) |
Emitted when the underlying DataStore throws an exception. | |
void | typeIdChanged (int typeId, QPrivateSignal) |
NOTIFY accessor for DataStoreModel::typeId. | |
void | editableChanged (bool editable, QPrivateSignal) |
NOTIFY accessor for DataStoreModel::editable. | |
Public Member Functions | |
DataStoreModel (QObject *parent=nullptr) | |
Constructs a model for the default setup. More... | |
DataStoreModel (const QString &setupName, QObject *parent=nullptr) | |
Constructs a model for the given setup. More... | |
DataStoreModel (DataStore *store, QObject *parent=nullptr) | |
Constructs a model on the given store. More... | |
DataStore * | store () const |
Returns the data store the model works on. More... | |
QString | setupName () const |
Returns the name of the setup this class operates on. More... | |
int | typeId () const |
READ accessor for DataStoreModel::typeId. | |
template<typename T > | |
void | setTypeId (bool resetColumns=true) |
WRITE accessor for DataStoreModel::typeId. | |
bool | isEditable () const |
READ accessor for DataStoreModel::editable. | |
QVariant | headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override |
Inherits QAbstractTableModel::headerData. | |
int | rowCount (const QModelIndex &parent=QModelIndex()) const override |
Inherits QAbstractTableModel::rowCount. | |
int | columnCount (const QModelIndex &parent) const override |
Inherits QAbstractTableModel::columnCount. | |
bool | canFetchMore (const QModelIndex &parent) const override |
Inherits QAbstractTableModel::canFetchMore. | |
void | fetchMore (const QModelIndex &parent) override |
Inherits QAbstractTableModel::fetchMore. | |
QModelIndex | index (int row, int column=0, const QModelIndex &parent=QModelIndex()) const override |
Inherits QAbstractTableModel::index. | |
Q_INVOKABLE QModelIndex | idIndex (const QString &id) const |
Returns the index of the item with the given id. | |
template<typename T > | |
QModelIndex | idIndex (const T &id) const |
Returns the index of the item with the given id. | |
Q_INVOKABLE QString | key (const QModelIndex &index) const |
Returns the key of the item at the given index. | |
template<typename T > | |
T | key (const QModelIndex &index) const |
Returns the key of the item at the given index. | |
QVariant | data (const QModelIndex &index, int role=Qt::DisplayRole) const override |
Inherits QAbstractTableModel::data. | |
bool | setData (const QModelIndex &index, const QVariant &value, int role) override |
Inherits QAbstractTableModel::setData. | |
Q_INVOKABLE QVariant | object (const QModelIndex &index) const |
Returns the object at the given index. More... | |
template<typename T > | |
T | object (const QModelIndex &index) const |
Returns the object at the given index. More... | |
Q_INVOKABLE QVariant | loadObject (const QModelIndex &index) const |
Loads the object at the given index from the store via DataStore::load. More... | |
template<typename T > | |
T | loadObject (const QModelIndex &index) const |
Loads the object at the given index from the store via DataStore::load. More... | |
Qt::ItemFlags | flags (const QModelIndex &index) const override |
Inherits QAbstractTableModel::flags. | |
QHash< int, QByteArray > | roleNames () const override |
Inherits QAbstractTableModel::roleNames. | |
int | addColumn (const QString &text) |
Add a new column with the given title. More... | |
int | addColumn (const QString &text, const char *propertyName) |
Add a new column and set the given property as its Qt::DisplayRole. More... | |
void | addRole (int column, int role, const char *propertyName) |
Adds the given property as a new role to the given column. More... | |
void | clearColumns () |
Removes all customly added columns and roles. | |
Public Member Functions inherited from QAbstractTableModel | |
virtual const QMetaObject * | metaObject () const const |
virtual void * | qt_metacast (const char *) |
virtual int | qt_metacall (QMetaObject::Call, int, void **) |
QAbstractTableModel (QObject *parent) | |
virtual QModelIndex | sibling (int row, int column, const QModelIndex &idx) const const override |
virtual bool | dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override |
Public Member Functions inherited from QAbstractItemModel | |
QAbstractItemModel (QObject *parent) | |
bool | hasIndex (int row, int column, const QModelIndex &parent) const const |
virtual QModelIndex | parent (const QModelIndex &index) const const=0 |
virtual bool | hasChildren (const QModelIndex &parent) const const |
virtual bool | setHeaderData (int section, Qt::Orientation orientation, const QVariant &value, int role) |
virtual QMap< int, QVariant > | itemData (const QModelIndex &index) const const |
virtual bool | setItemData (const QModelIndex &index, const QMap< int, QVariant > &roles) |
virtual QStringList | mimeTypes () const const |
virtual QMimeData * | mimeData (const QModelIndexList &indexes) const const |
virtual bool | canDropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const const |
virtual Qt::DropActions | supportedDropActions () const const |
virtual Qt::DropActions | supportedDragActions () const const |
void | setSupportedDragActions (Qt::DropActions actions) |
virtual bool | insertRows (int row, int count, const QModelIndex &parent) |
virtual bool | insertColumns (int column, int count, const QModelIndex &parent) |
virtual bool | removeRows (int row, int count, const QModelIndex &parent) |
virtual bool | removeColumns (int column, int count, const QModelIndex &parent) |
virtual bool | moveRows (const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) |
virtual bool | moveColumns (const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent, int destinationChild) |
bool | insertRow (int row, const QModelIndex &parent) |
bool | insertColumn (int column, const QModelIndex &parent) |
bool | removeRow (int row, const QModelIndex &parent) |
bool | removeColumn (int column, const QModelIndex &parent) |
bool | moveRow (const QModelIndex &sourceParent, int sourceRow, const QModelIndex &destinationParent, int destinationChild) |
bool | moveColumn (const QModelIndex &sourceParent, int sourceColumn, const QModelIndex &destinationParent, int destinationChild) |
virtual void | sort (int column, Qt::SortOrder order) |
virtual QModelIndex | buddy (const QModelIndex &index) const const |
virtual QModelIndexList | match (const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const const |
virtual QSize | span (const QModelIndex &index) const const |
bool | checkIndex (const QModelIndex &index, QAbstractItemModel::CheckIndexOptions options) const const |
void | dataChanged (const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles) |
void | headerDataChanged (Qt::Orientation orientation, int first, int last) |
void | layoutChanged (const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint) |
void | layoutAboutToBeChanged (const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint) |
void | rowsAboutToBeInserted (const QModelIndex &parent, int start, int end) |
void | rowsInserted (const QModelIndex &parent, int first, int last) |
void | rowsAboutToBeRemoved (const QModelIndex &parent, int first, int last) |
void | rowsRemoved (const QModelIndex &parent, int first, int last) |
void | columnsAboutToBeInserted (const QModelIndex &parent, int first, int last) |
void | columnsInserted (const QModelIndex &parent, int first, int last) |
void | columnsAboutToBeRemoved (const QModelIndex &parent, int first, int last) |
void | columnsRemoved (const QModelIndex &parent, int first, int last) |
void | modelAboutToBeReset () |
void | modelReset () |
void | rowsAboutToBeMoved (const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow) |
void | rowsMoved (const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row) |
void | columnsAboutToBeMoved (const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn) |
void | columnsMoved (const QModelIndex &parent, int start, int end, const QModelIndex &destination, int column) |
virtual bool | submit () |
virtual void | revert () |
Public Member Functions inherited from QObject | |
QObject (QObject *parent) | |
virtual bool | event (QEvent *e) |
virtual bool | eventFilter (QObject *watched, QEvent *event) |
QString | objectName () const const |
void | setObjectName (const QString &name) |
bool | isWidgetType () const const |
bool | isWindowType () const const |
bool | signalsBlocked () const const |
bool | blockSignals (bool block) |
QThread * | thread () const const |
void | moveToThread (QThread *targetThread) |
int | startTimer (int interval, Qt::TimerType timerType) |
int | startTimer (std::chrono::milliseconds time, Qt::TimerType timerType) |
void | killTimer (int id) |
T | findChild (const QString &name, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QString &name, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QRegExp ®Exp, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const |
const QObjectList & | children () const const |
void | setParent (QObject *parent) |
void | installEventFilter (QObject *filterObj) |
void | removeEventFilter (QObject *obj) |
QMetaObject::Connection | connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const |
bool | disconnect (const char *signal, const QObject *receiver, const char *method) const const |
bool | disconnect (const QObject *receiver, const char *method) const const |
void | dumpObjectTree () |
void | dumpObjectInfo () |
void | dumpObjectTree () const const |
void | dumpObjectInfo () const const |
bool | setProperty (const char *name, const QVariant &value) |
QVariant | property (const char *name) const const |
QList< QByteArray > | dynamicPropertyNames () const const |
void | destroyed (QObject *obj) |
void | objectNameChanged (const QString &objectName) |
QObject * | parent () const const |
bool | inherits (const char *className) const const |
void | deleteLater () |
Properties | |
int | typeId |
Holds the type the model loads data for. More... | |
bool | editable |
Specifies whether the model items can be edited. More... | |
Properties inherited from QObject | |
objectName | |
Additional Inherited Members | |
Static Public Member Functions inherited from QAbstractTableModel | |
QString | tr (const char *s, const char *c, int n) |
QString | trUtf8 (const char *s, const char *c, int n) |
Static Public Member Functions inherited from QAbstractItemModel | |
QString | tr (const char *s, const char *c, int n) |
QString | trUtf8 (const char *s, const char *c, int n) |
Static Public Member Functions inherited from QObject | |
QString | tr (const char *sourceText, const char *disambiguation, int n) |
QString | trUtf8 (const char *sourceText, const char *disambiguation, int n) |
QMetaObject::Connection | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, Functor functor) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type) |
bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method) |
bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method) |
bool | disconnect (const QMetaObject::Connection &connection) |
bool | disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method) |
Public Attributes inherited from QAbstractItemModel | |
typedef | CheckIndexOptions |
Protected Member Functions inherited from QAbstractItemModel | |
void | resetInternalData () |
QModelIndex | createIndex (int row, int column, void *ptr) const const |
QModelIndex | createIndex (int row, int column, quintptr id) const const |
void | beginInsertRows (const QModelIndex &parent, int first, int last) |
void | endInsertRows () |
void | beginRemoveRows (const QModelIndex &parent, int first, int last) |
void | endRemoveRows () |
bool | beginMoveRows (const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild) |
void | endMoveRows () |
void | beginInsertColumns (const QModelIndex &parent, int first, int last) |
void | endInsertColumns () |
void | beginRemoveColumns (const QModelIndex &parent, int first, int last) |
void | endRemoveColumns () |
bool | beginMoveColumns (const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild) |
void | endMoveColumns () |
void | reset () |
void | beginResetModel () |
void | endResetModel () |
void | changePersistentIndex (const QModelIndex &from, const QModelIndex &to) |
void | changePersistentIndexList (const QModelIndexList &from, const QModelIndexList &to) |
QModelIndexList | persistentIndexList () const const |
void | setRoleNames (const QHash< int, QByteArray > &roleNames) |
Protected Member Functions inherited from QObject | |
QObject * | sender () const const |
int | senderSignalIndex () const const |
int | receivers (const char *signal) const const |
bool | isSignalConnected (const QMetaMethod &signal) const const |
virtual void | timerEvent (QTimerEvent *event) |
virtual void | childEvent (QChildEvent *event) |
virtual void | customEvent (QEvent *event) |
virtual void | connectNotify (const QMetaMethod &signal) |
virtual void | disconnectNotify (const QMetaMethod &signal) |
A passive item model for a datasync data store.
This model can be used to display the data from a DataStore as model. The model itself is typically readonly, as the data is updated automatically as the contents in the store changes. Check out the example below to see how to use it.
After creating the model, you can set the datatype it should display via setTypeId(). This will reset the model and let it load the data from it's store. The model is generic and can handle any object or gadget type. The properties of those objects/gadgets are translated to display roles, and each object/gadget is represented by one row in the model.
This makes the model easy to use with QML, as there you access data via roles, but requires additional code to display it via a QTreeView or similar. Here, another project of mine can help: QObjectListModel Provides a class called QObjectProxyModel
. This class can map roles to columns, making it possible to display a model like this one in widgets properly. The example below shows how to.
To "modify" the model, use one of the datasync stores and insert, updated or remove data. Once the change is successfully done in the engine, the model updates automatically. Sorting the model itself is not possible, but you can make use of a QSortFilterProxyModel to display the data sorted.
The model is readonly by default, but you can make exising items editable via DataStoreModel::editable. This does not allow inserting or removing items via the model, but allows you to change properties via the setData() method.
Items can be loaded from the model use loadObject(). This allows you to get the item at a specific index and pass it to other components, update it and other. This method loads a new instance from the store, and this is safe in any case. With the object() method this can be done faster, but is not** safe for all types. Read the DataStoreModel::object documentation for details.
Assuming your data class looks like this:
You can instanciate a model for this data as follows:
One additional feature of the model is, that it can also serve as model for table/tree views by delegating properties to multiple columns. This way you can use the model from QML by using the property names, and from widgets by delegating the properties to extra columns. For the above sample, the following code will result in a model with 2 columns, presenting the name and age properties as the Qt::DisplayRole of each column:
Definition at line 13 of file datastoremodel.h.
|
explicit |
Constructs a model for the default setup.
parent | The parent object |
SetupDoesNotExistException | Thrown if the default setup was not created yet |
|
explicit |
Constructs a model for the given setup.
setupName | The name of the setup to connect to |
parent | The parent object |
SetupDoesNotExistException | Thrown if the given setup was not created yet |
|
explicit |
Constructs a model on the given store.
store | The store to be used by the model |
parent | The parent object |
Add a new column with the given title.
text | The display name of the column header |
The method add a column to the model that can be used to present propeties from the underlying data as specific roles.
Add a new column and set the given property as its Qt::DisplayRole.
text | The display name of the column header |
propertyName | The name of the property to presented as the display role of that column |
This method is basically a shortcut for:
QVariant QtDataSync::DataStoreModel::addRole | ( | int | column, |
int | role, | ||
const char * | propertyName | ||
) |
Adds the given property as a new role to the given column.
column | The existing column to add the role to |
role | The role that should be added to the column |
propertyName | The name of the property to presented as the give role of that column |
This method adds a new role to an existing column. The new role will simply the the value of the given property of the object in each row. It's basically a delegate to the properties role.
QVariant QtDataSync::DataStoreModel::loadObject | ( | const QModelIndex & | index | ) | const |
Loads the object at the given index from the store via DataStore::load.
index | The model index to return the object for |
NoDataException | In case no dataset for the given type and key was found |
LocalStoreException | In case of an internal error |
The method uses the store() to load a new instance of the data using the very key of the dataset the given index identifies. The returned data is owned by the caller. For more details, check DataStore::load.
T QtDataSync::DataStoreModel::loadObject | ( | const QModelIndex & | index | ) | const |
Loads the object at the given index from the store via DataStore::load.
T | The type of object to return. Must match DataStoreModel::typeId |
index | The model index to return the object for |
NoDataException | In case no dataset for the given type and key was found |
LocalStoreException | In case of an internal error |
The method uses the store() to load a new instance of the data using the very key of the dataset the given index identifies. The returned data is owned by the caller. For more details, check DataStore::load.
Definition at line 165 of file datastoremodel.h.
QVariant QtDataSync::DataStoreModel::object | ( | const QModelIndex & | index | ) | const |
Returns the object at the given index.
index | The model index to return the object for |
|
inline |
Returns the object at the given index.
T | The type of object to return. Must match DataStoreModel::typeId |
index | The model index to return the object for |
Definition at line 158 of file datastoremodel.h.
QString QtDataSync::DataStoreModel::setupName | ( | ) | const |
Returns the name of the setup this class operates on.
QtDataSync::DataStoreModel::store | ( | ) | const |
Returns the data store the model works on.
|
readwrite |
Specifies whether the model items can be edited.
Default: false
By default, the model is not editable and servers as a normal read only model. By enabeling this property, the setData() method allows modification of items. These modifications will be automatically passed to the store and thus are permanent data changes.
Accessors | |
---|---|
READ | isEditable() |
WRITE | setEditable() |
NOTIFY | editableChanged() |
Definition at line 21 of file datastoremodel.h.
|
readwrite |
Holds the type the model loads data for.
Default: QMetaType::UnknownType
The type id is essential for the model, and defines what data should be loaded. When changing the property, the model resets and the loads all data from it's store for the given type, and then reacts on changes for that type.
Setting it to an unknown or invalid type will lead to errors. The type should be set before passing the model to a view or proxy model, as these need the models roles to be defined to work properly.
Accessors | |
---|---|
READ | typeId() |
WRITE | setTypeId() |
NOTIFY | typeIdChanged() |
Definition at line 19 of file datastoremodel.h.