I have one persistent class, call it "ProjectDefaults", of which there exists only one instance, of which I already have a valid ProjectDefaults_ptr.
One instance, so I assume that there is only 1 row into your database (
qxFetchAll() will return only 1 instance).
So, with the QxModelView module, you could write exactly as the FAQ :
- Code: Select all
// Create a model and fetch all data from database
qx::IxModel * pModel = new qx::QxModel<ProjectDefaults>();
pModel->qxFetchAll();
// Associate the model to a QML view and display it
QQuickView qmlView;
qmlView.rootContext()->setContextProperty("myModel", pModel);
qmlView.setSource(QUrl("qrc:/documents/main.qml"));
qmlView.show();
Then, in the QML view, all properties of your
ProjectDefaults class are available !
the question about property setters directly accessing the database (i.e., no "save" button)
To do this kind of things, I would recommend to create your own template class wich inherits from
qx::QxModel<T>.
Then, in your template class, you just have to override the "
virtual bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole)" method.
I write it quickly but your template class could be something like this :
- Code: Select all
template <class T>
class MyModelSaveOnSetter : public qx::QxModel<T>
{
virtual bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole)
{
bool bSetDataOk = qx::QxModel<T>::setData(index, value, role); // call base class setter
if (bSetDataOk) { this->qxSave(); }
return bSetDataOk;
}
};
Then, to create your model :
- Code: Select all
qx::IxModel * pModel = new MyModelSaveOnSetter<ProjectDefaults>();