qx::QxModel<T> : all classes registered into QxOrm context can be used with Qt model/view architecture (Qt widgets and/or QML views)
More...
Public Types |
| enum | { qx_is_valid = qx::trait::is_qx_registered<T>::value
} |
| typedef boost::shared_ptr< T > | type_ptr |
typedef
qx::trait::get_primary_key< T >
::type | type_primary_key |
typedef qx::QxCollection
< type_primary_key, type_ptr > | type_collection |
Public Member Functions |
| | QxModel (QObject *parent=0) |
| | QxModel (qx::IxModel *other, QObject *parent) |
| virtual | ~QxModel () |
| virtual QVariant | data (const QModelIndex &index, int role=Qt::DisplayRole) const |
| virtual bool | setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) |
| virtual bool | insertRows (int row, int count, const QModelIndex &parent=QModelIndex()) |
| virtual long | qxCount (const qx::QxSqlQuery &query=qx::QxSqlQuery(), QSqlDatabase *pDatabase=NULL) |
| virtual QSqlError | qxCount (long &lCount, const qx::QxSqlQuery &query=qx::QxSqlQuery(), QSqlDatabase *pDatabase=NULL) |
| virtual QSqlError | qxFetchById (const QVariant &id, const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL) |
| virtual QSqlError | qxFetchAll (const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL) |
| virtual QSqlError | qxFetchByQuery (const qx::QxSqlQuery &query, const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL) |
| virtual QSqlError | qxInsert (const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL) |
| virtual QSqlError | qxUpdate (const qx::QxSqlQuery &query=qx::QxSqlQuery(), const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL) |
| virtual QSqlError | qxSave (const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL) |
| virtual QSqlError | qxSaveRow (int row, const QStringList &relation=QStringList(), QSqlDatabase *pDatabase=NULL) |
| virtual QSqlError | qxDeleteById (const QVariant &id, QSqlDatabase *pDatabase=NULL) |
| virtual QSqlError | qxDeleteAll (QSqlDatabase *pDatabase=NULL) |
| virtual QSqlError | qxDeleteByQuery (const qx::QxSqlQuery &query, QSqlDatabase *pDatabase=NULL) |
| virtual QSqlError | qxDestroyById (const QVariant &id, QSqlDatabase *pDatabase=NULL) |
| virtual QSqlError | qxDestroyAll (QSqlDatabase *pDatabase=NULL) |
| virtual QSqlError | qxDestroyByQuery (const qx::QxSqlQuery &query, QSqlDatabase *pDatabase=NULL) |
| virtual QSqlError | qxExecuteQuery (qx::QxSqlQuery &query, QSqlDatabase *pDatabase=NULL) |
| virtual qx_bool | qxExist (const QVariant &id, QSqlDatabase *pDatabase=NULL) |
| virtual qx::QxInvalidValueX | qxValidate (const QStringList &groups=QStringList()) |
Protected Member Functions |
| void | init () |
| void | initFrom (qx::IxModel *pOther) |
Protected Attributes |
| type_collection | m_model |
| | Model associated to a class registered into QxOrm context.
|
| long | m_lManualInsertIndex |
| | Index to insert manually items to the collection.
|
Friends |
| struct | qx::model_view::detail::QxNestedModel< T > |
| struct | qx::model_view::detail::QxNestedModel_Generic< T > |
| struct | qx::model_view::detail::QxNestedModel_Container |
template<class T>
class qx::QxModel< T >
qx::QxModel<T> : all classes registered into QxOrm context can be used with Qt model/view architecture (Qt widgets and/or QML views)
QxModelView module provides an easy way to work with Qt model/view engine with all classes registered into QxOrm context :
- Qt widgets : QTableView or QListView for example to display/modify a database table content ;
- QML : each property defined in QxOrm context is exposed to QML engine : QxModelView module makes easier integration between QML and databases.
qx::IxModel interface provides a generic way for all models linked to persistents classes registered into QxOrm context. All methods of this class prefixed by qx call functions from qx::dao namespace and then communicate with database.
The qxBlogModelView sample project in ./test/ directory of QxOrm package shows how to create quickly a model and associate it to the Qt model/view engine (first with a Qt widget, then with a QML view).
1- Here is an example to display/modify data from 'author' table (go to qxBlog tutorial for 'author' class definition) in a QTableView :
2- Here is another example in QML (with Qt5, QxModelView module works fine with Qt4 too) :
qx::IxModel * pModel = new qx::QxModel<author>();
pModel->qxFetchAll();
QQuickView qmlView;
qmlView.rootContext()->setContextProperty("myModel", pModel);
qmlView.setSource(QUrl("qrc:/documents/main.qml"));
qmlView.show();
And here is the 'main.qml' file content :
import QtQuick 2.1
import QtQuick.Controls 1.0
Item {
width: 400
height: 300
Row {
height: 20
spacing: 20
Button {
text: "Clear"
onClicked: myModel.clear()
}
Button {
text: "Fetch All"
onClicked: myModel.qxFetchAll_()
}
Button {
text: "Save"
onClicked: myModel.qxSave_()
}
}
ListView {
y: 30
height: 270
model: myModel
delegate: Row {
height: 20
spacing: 10
Text { text: "id: " + author_id }
TextField {
text: name
onTextChanged: name = text
}
}
}
}
As you can see in the 'main.qml' file, 'author_id' and 'name' properties of 'author' model ('myModel' variable) can be automatically read and write (because they are registered into QxOrm context). Moreover, qx::IxModel interface provides a list of methods for QML side (Q_INVOKABLE) to communicate with database : for example, the 'Save' button will save the model in database without having to write a C++ function.
Note : a QxEntityEditor plugin generates automatically the code to manage models with relationships. Then it is possible to work with nested C++ models.
Definition at line 153 of file QxModel.h.