Important: Please read the Qt Code of Conduct -

[ABANDONED] Best way to design model + TableModel using ORM

  • I've abandoned my question because it was badly explained. I rewrote it on "stackoverflow": because it's not Qt specific and I'm thinking if I write it in the "brainstorm forum": soon.

    I'm on a really simple app, using QAbstractTableModel and an ORM/Sqlite (first time using ORM) (I'm on QDjango for now, because except ODB - which has issues on OS X 10.10 - I didn't find another simple and good one). My question is general, using ODB, QDjango or another ORM should lead to the same answers.

    Here is my model class (simplified):
    @class Category : public QDjangoModel
    quint8 m_id;
    QString m_name;

        [...] // Constructors, getters, setters, ...
        bool         save(void);


    I'm also subclassing QAbstractTableModel to use a given set of model (from the ORM, here QDjangoQuerySet<Category>) to use it in a QTableView.

    I need help to understand how to correctly design these 2 classes (or more if needed) to be more confortable with an ORM?


    My first idea was to include a QDjangoQuerySet in my CategoryTableModel and subclass data() and setData() to directly use the given query set, which operate directly on the database (lazy loading or not, I don't know). My first problem with this idea, is that table models are called cells by cells, which is reaaaaaally bad for the database because it will also call the database field by field (or row by row if I fetch an entire row first).

    Consequences of the first idea, I had a second one: Instead of giving the database access to my table model, I can just fetch the Category table and store a QList<Category> inside my table model. Good idea we might think (except those who have faced this situation), but this idea causes 2 problems: First is that I can't track changes (by QAbstractTableModel::setData()) to update the database, and updating a list if none has been modified seems ugly. Second issue, is the list size which could be 10 for categories, but thousands for invoices for instance.

    Then, third idea was reimplement canFetch() and fetchMore(), but it's really complicated (at least with QDjango, because of some methods not const and other things) and I was beginning to write again the ORM work, which I shouldn't.

    So, I'm thinking it's a good idea to ask the Qt's community about that. When subclassing a QAbstractModel, what is the best way to use the ORM? Where should be stored the "database access", how to track changes to update them, and how to correctly store and access internal data of my model-list then?

    Thanks for your help.

  • Hello,

    "QxOrm library": provides out of the box "the QxModelView module": : with this module, you can use each class registered into QxOrm context (so mapped to a database table) with model/view framework of Qt (and QML views too !).

    There is an explanation in the FAQ here : (and a sample project in the ./test/qxBlogModelView/ directory of QxOrm package).

    And if you want to work with relationships and the Qt model/view framework, you can use "QxEntityEditor": (the graphic editor for QxOrm library) to generate automatically the source code (with the C++ model/view export plugin).

  • Thank you for your advices I note qxorm (which I've explored a little)

Log in to reply