Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Register C++ class derived from QAbstractListModel
Forum Updated to NodeBB v4.3 + New Features

Register C++ class derived from QAbstractListModel

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 554 Views 1 Watching
  • 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.
  • R Offline
    R Offline
    realroot
    wrote on last edited by
    #1

    I am following the examples here: https://www.qt.io/product/qt6/qml-book/ch17-qtcpp-cpp-models#a-simple-model.

    MyClass.h

    class MyClass : public QAbstractListModel
    {
    ...
    

    But I cannot register the class:

    error: static assertion failed: It is not possible to register an abstract type with qmlRegisterType. Maybe you wanted qmlRegisterUncreatableType or qmlRegisterInterface?
    

    It works if I derive from QObject, I can compile the code and use the class in main.qml.
    I also tried to use QML_ELEMENT.

    The class is used for a ListView, as the example so I am supposed to use a QAbstractListModel right?

    Why can't I use it?

    sierdzioS 1 Reply Last reply
    0
    • R realroot

      I am following the examples here: https://www.qt.io/product/qt6/qml-book/ch17-qtcpp-cpp-models#a-simple-model.

      MyClass.h

      class MyClass : public QAbstractListModel
      {
      ...
      

      But I cannot register the class:

      error: static assertion failed: It is not possible to register an abstract type with qmlRegisterType. Maybe you wanted qmlRegisterUncreatableType or qmlRegisterInterface?
      

      It works if I derive from QObject, I can compile the code and use the class in main.qml.
      I also tried to use QML_ELEMENT.

      The class is used for a ListView, as the example so I am supposed to use a QAbstractListModel right?

      Why can't I use it?

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @realroot said in Register C++ class derived from QAbstractListModel:

      Why can't I use it?

      Impossible to say for sure without seeing more code.

      My guesses:

      • you forgot about Q_OBJECT macro
      • you did not reimplement abstract methods from QAbstractListModel (this is precisely what this error tells you)

      As the documentation say:

      When subclassing QAbstractListModel, you must provide implementations of the rowCount() and data() functions

      (Z(:^

      R 1 Reply Last reply
      0
      • sierdzioS sierdzio

        @realroot said in Register C++ class derived from QAbstractListModel:

        Why can't I use it?

        Impossible to say for sure without seeing more code.

        My guesses:

        • you forgot about Q_OBJECT macro
        • you did not reimplement abstract methods from QAbstractListModel (this is precisely what this error tells you)

        As the documentation say:

        When subclassing QAbstractListModel, you must provide implementations of the rowCount() and data() functions

        R Offline
        R Offline
        realroot
        wrote on last edited by realroot
        #3

        The error message is not so clear.

        I did not make the implementations of the rowCount() and data() functions, thanks.

        I am replacing the configuration file that it is a QML file.
        I am not sure about the implementation.

        I have a ListView whose elements are ListView.
        ListViews have a few properties and the inner one has ListElements:

        import QtQuick
        
        Item {
            id: config
        
            //Inner ListView
            Item {
                id: w001
                property bool defAutomatic: true
                property int sets: 1
                property string woName: "30 seconds"
                property bool initPrep: true
        
                property int exTotal: w001timers.count
                property alias timers: w001timers
                property int setTotal: w001timers.setTotal
                property int woTotal: w001timers.woTotal
        
                ListModel {
                    id: w001timers
                    property int sets: w001.sets
                    property string woName: w001.woName
                    property bool initPrep: w001.initPrep
                    property int setTotal: {
                        for (var i = 0; i < count; i++) {
                            setTotal += get(i).countdownValue;
                        }
                    }
                    property int woTotal: setTotal * sets
        
                    ListElement {
                        automatic: true
                        countdownValue: 30
                        timerName: "go"
                        prepare: false
                    }
                }
            }
        ...
        

        I am adding a C++ back end to save/load/edit the configuration, using only QML works but that looks like suitable for C++.
        I thinking about saving it as json.

        Any suggestion? I am a beginner.
        Should I submit a new topic?

        1 Reply Last reply
        0
        • R realroot has marked this topic as solved on
        • R realroot has marked this topic as solved on
        • R Offline
          R Offline
          realroot
          wrote on last edited by
          #4

          Moved here https://forum.qt.io/topic/154442/convert-qml-file-to-json.

          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