Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Render nested QAbstractItemModels



  • I'm adding Qt support to an upstream C++ library that has its own model types by making the upstream library's existing types inherit QAbstractItemModel with the intention of using these models in a QML application. Everything is going well for the simple models, however some of the upstream classes are nested, and the resulting structure looks something like this:

    class MyClass1 : QAbstractItemModel
    {
        Q_OBJECT
        Q_PROPERTY(int a READ get_a NOTIFY updated_a)
    
    public:    
        enum Roles {
            IDRole = Qt::UserRole + 1,          // QString
            NameRole = Qt::UserRole + 2,        // QString
            SubmodelRole = Qt::UserRole + 3,    // MyClass2*
        };
        
        Q_INVOKABLE bool function1(int, bool);
        int get_a();
        // QAbstractItemModel overridden functions omitted
    
    signals:
        void signal1();
        void updated_a();
    };
    
    class MyClass2 : QAbstractItemModel
    {
        Q_OBJECT
        Q_PROPERTY(QString b READ get_b NOTIFY updated_b)
        Q_PROPERTY(int c READ get_c NOTIFY updated_c)
    
    public:    
        enum Roles {
            AmountRole = Qt::UserRole + 1,      // int
            SubmodelRole = Qt::UserRole + 2,    // MyClass3*
        };
        
        Q_INVOKABLE bool function2(bool);
        QString get_b();
        int get_c();
        // QAbstractItemModel overridden functions omitted
    
    signals:
        void signal2();
        void updated_b();
        void updated_c();
    };
    
    class MyClass3 : QAbstractItemModel
    {
        Q_OBJECT
    
    public:    
        enum Roles {
            DateRole = Qt::UserRole + 1,          // QDateTime
            DescriptionRole = Qt::UserRole + 2,   // QString
        };
        
        Q_INVOKABLE bool function3(QString);
        // QAbstractItemModel overridden functions omitted
    
    signals:
        void signal3();
    };
    

    Is it possible to make a QML page that fully displays a MyClass1 model, including any submodels that may be present?


Log in to reply