Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QAbstractItemModel and QSqlQuery... hox to use it together ?
I see a very good video from Qt team around use of QAbstractItemModel (in 2011 the video on youtube around "deep dive part 6/6 model/view" here is the video link: https://www.youtube.com/watch?v=f3gkpd25BqE (and you can see the code around minute 35...
so... because i'm beginner, i not understand (and would like to do it) how to catch data from SQL database with QAbstractItemModel... also, i understand (i hope) that this could be the way to interface SQL data with the QTreeView by construct my custom model by this way.
I search a link for try to learn it... i thnk i need to override the data() member with QSqlQuery, but i'm not sure... also, maybe i need to do something on the constructor to link the database directly... it's confuse for me on these points.
Does someone could try to help me to understand this or provide a little exemple ?
Also, i would like to (but i'm not sure it is a great design patern, because lake of knowledge on it) construct a "setQuery(const QList<QString> &fields, const QString &table)" for be able to pass the required query to link to the model. Something like: myCustomModel->setQuery(my_fields_names, my_table);
i'm not sure to be also on the good design patern way to use it well...
andre last edited by
Wouldn't it be a good idea to start with QSqlQueryModel instead?
I try it allready.
I make it working well... and also QSqlRelationnalTableModel.
So i gone far more than just a try.
But i had a problem crash come from qsqldriver (i use it for postgresql access).... and loose time for nothing more by this way. It was a great way also, i'm sorry to have the sqldriver crash (and i'm not the sufficient level to debug the driver code too). Someone tell me that the drivers has not been touch or maintain for long time now, and works fine for many situations... but also i need a best design patern.
but the fact is that i would like to use a QAbstractItemModel drectly with a good design patern. I prefer to loose time first by this way to learn, than loose more time after to do it at the end.
I really need to have something with good design patern, my application need it also.
But... i think for you this is not to difficult to do (call datas from database inisde a QAbstractItemModel).
And then, i think this couyld be the best way (nut maybe i'm wrong... i'm convainct by the great video i saw around QAbstractItemModel, and by you too).
I also need after that to be able to point the SQL request by a limitation depend of the real view (from event->pos() from viewport by a signal event).
but first (who is really the first i will do) is to be able to do a great design patern just for call datas from QAbstractItemModel.
Do you want to help me to do that ?
qxorm last edited by
If you are looking for an example of models connected to database, you can take a look at "the QxModelView module of QxOrm library":http://www.qxorm.com/doxygen/html/group___qx_model_view.html : this is an implementation of the QAbstractItemModel Qt class and allows to display in QML views (and Qt Widgets too) all classes/properties registered into QxOrm context.
The source code is available in the "QxOrm package":http://www.qxorm.com/qxorm_en/home.html.
There is a FAQ here : http://www.qxorm.com/qxorm_en/faq.html#faq_300
And there is a sample project in the ./test/qxBlogModelView/ directory of QxOrm package.
And with QxEntityEditor, you can also generate the source code for nested models to manage relationships between entities (1-n, n-1, n-n, 1-1).
thanks qxorm, i download the source for have a look around this...
and then, this seems to be a really powerfull tool construct with Qt... rally nice also. I think i could find more than i need inside.
thanks a lot.
ok qxorm... this way to do is definitly not my level, for understand this code alone, i maybe need 10 years or more... (i'm beginner in Qt and in C++ too... and alone to code my application...). It is sympathic to try to help by provide this code source, but i not understand more after read carrefuly this code.
i think someone could read and understand quickly this code could also do what i need to do without ask questions here... for sure.
if someone who know could remember the time he was beginner and provide an "humble" exemple, maybe this would help me more to try to do it well.
thanks for your help again.... if i find, i will provide some information for some other beginner who want to learn.