Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QML doesn't "see" my subclass of QAbstructListModel
Forum Update on Monday, May 27th 2025

QML doesn't "see" my subclass of QAbstructListModel

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 254 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Curtwagner1984
    wrote on last edited by
    #1

    Hello,
    following this post, I've created a subclass of QAbstructListItem and now I'm trying to make QML interact with it by passing it to as a context property.

    I can successfully pass a pointer to qml, (As in I can print the address I passed as text), but the QML ListView doesn't seem to recognize it as a valid model.)

    I tried to put a breakpoint on some of the model methods such as rowCount and data and it seems they are never called.

    I also tried registering the model with qRegisterMetaType which had no effect

    indicatorlistmodel.h

    class IndicatorListModel: public QAbstractListModel
    {
            Q_OBJECT
    
        public:
    
            explicit IndicatorListModel(QObject* parent = nullptr);
            IndicatorListModel(const IndicatorListModel& other);
            ~IndicatorListModel();
            
            // QAbstractItemModel interface
            int                    rowCount(const QModelIndex& parent) const;
            QVariant               data(const QModelIndex& index, int role) const;
            QHash<int, QByteArray> roleNames() const;
    
            void                   SetModelData(QList<IndicatorBase*> dataList);
    
        private:
    
            QList<IndicatorBase*> _indicatorDataList;
    };
    

    In my main file, I generate some data for this class like this:

    main.cpp

        IndicatorListModel listModel;
    
        QList<IndicatorBase *> indicatorList;
        indicatorList.append(new LedSingleIndicator(true,"RAM 1"));
        indicatorList.append(new LedSingleIndicator(true,"RAM 2"));
        indicatorList.append(new LedSingleIndicator(true,"RAM 3"));
    
        listModel.SetModelData(indicatorList);
    
        qDebug() << "Model size: " << listModel.rowCount(QModelIndex()); //prints out "Model size:  3"
    

    I then pass the model to qml as a context property like this:

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QQmlContext* context = engine.rootContext();
    context->setContextProperty("model",&listModel);
    engine.load(url);
    return app.exec();
    

    In my main QML there is a simple ListView:

    
    Window {
        id:mainwindow
        width: 640
        height: 480
        visible: true    
        title: qsTr("Hello World")
    
        Component.onCompleted: {
            console.debug("Component Main Window loaded..");
        }
        color:"green"
    
        Text{
           anchors.centerIn: parent
           text: "Model is: " + model //prints `Model is: IndicatorListModel(0xa8fc70)`
        }
    
        ListView{
            id: listView
            width: 400
            height: 400
            model: model
    
            delegate: Text{
                text: "Sample Text"
    
            }
        }
    
    //    ListView{
    //        id: listview
    //        anchors.fill: parent
    //        required model
    
    //        delegate: Text{
    //            required property string labelRole
    //            text: "Indicator Name: "  + labelRole
    //        }
    //    }
    }
    
    

    QML Window looks like this:
    alt text

    I can't figure out why the ListView doesn't recognize my model as a proper model. Any help would be appreciated.

    J.HilkJ 1 Reply Last reply
    0
    • C Offline
      C Offline
      Curtwagner1984
      wrote on last edited by
      #2

      OK, so apparently the name model is the cause of my problem.

      Doesn't Work:

      context->setContextProperty("model",&listModel);
      

      Works:

      context->setContextProperty("myModel",&listModel);
      
      1 Reply Last reply
      1
      • C Curtwagner1984

        Hello,
        following this post, I've created a subclass of QAbstructListItem and now I'm trying to make QML interact with it by passing it to as a context property.

        I can successfully pass a pointer to qml, (As in I can print the address I passed as text), but the QML ListView doesn't seem to recognize it as a valid model.)

        I tried to put a breakpoint on some of the model methods such as rowCount and data and it seems they are never called.

        I also tried registering the model with qRegisterMetaType which had no effect

        indicatorlistmodel.h

        class IndicatorListModel: public QAbstractListModel
        {
                Q_OBJECT
        
            public:
        
                explicit IndicatorListModel(QObject* parent = nullptr);
                IndicatorListModel(const IndicatorListModel& other);
                ~IndicatorListModel();
                
                // QAbstractItemModel interface
                int                    rowCount(const QModelIndex& parent) const;
                QVariant               data(const QModelIndex& index, int role) const;
                QHash<int, QByteArray> roleNames() const;
        
                void                   SetModelData(QList<IndicatorBase*> dataList);
        
            private:
        
                QList<IndicatorBase*> _indicatorDataList;
        };
        

        In my main file, I generate some data for this class like this:

        main.cpp

            IndicatorListModel listModel;
        
            QList<IndicatorBase *> indicatorList;
            indicatorList.append(new LedSingleIndicator(true,"RAM 1"));
            indicatorList.append(new LedSingleIndicator(true,"RAM 2"));
            indicatorList.append(new LedSingleIndicator(true,"RAM 3"));
        
            listModel.SetModelData(indicatorList);
        
            qDebug() << "Model size: " << listModel.rowCount(QModelIndex()); //prints out "Model size:  3"
        

        I then pass the model to qml as a context property like this:

        QQmlApplicationEngine engine;
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QQmlContext* context = engine.rootContext();
        context->setContextProperty("model",&listModel);
        engine.load(url);
        return app.exec();
        

        In my main QML there is a simple ListView:

        
        Window {
            id:mainwindow
            width: 640
            height: 480
            visible: true    
            title: qsTr("Hello World")
        
            Component.onCompleted: {
                console.debug("Component Main Window loaded..");
            }
            color:"green"
        
            Text{
               anchors.centerIn: parent
               text: "Model is: " + model //prints `Model is: IndicatorListModel(0xa8fc70)`
            }
        
            ListView{
                id: listView
                width: 400
                height: 400
                model: model
        
                delegate: Text{
                    text: "Sample Text"
        
                }
            }
        
        //    ListView{
        //        id: listview
        //        anchors.fill: parent
        //        required model
        
        //        delegate: Text{
        //            required property string labelRole
        //            text: "Indicator Name: "  + labelRole
        //        }
        //    }
        }
        
        

        QML Window looks like this:
        alt text

        I can't figure out why the ListView doesn't recognize my model as a proper model. Any help would be appreciated.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Curtwagner1984 additionally to the really unfortunate naming you choose, please move your function to the correct section of your class and mark them as override so that you're sure, that you are actually overriding the correct functions:

        public:
            int rowCount(const QModelIndex &parent = QModelIndex()) const override;
            QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
        
        protected:
           QHash<int, QByteArray> roleNames() const override;
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        C 1 Reply Last reply
        2
        • J.HilkJ J.Hilk

          @Curtwagner1984 additionally to the really unfortunate naming you choose, please move your function to the correct section of your class and mark them as override so that you're sure, that you are actually overriding the correct functions:

          public:
              int rowCount(const QModelIndex &parent = QModelIndex()) const override;
              QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
          
          protected:
             QHash<int, QByteArray> roleNames() const override;
          
          C Offline
          C Offline
          Curtwagner1984
          wrote on last edited by
          #4

          @J-Hilk
          Will do, Thank you!

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved