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. Updating QAbstractItemModel: Model is null although begin/endResetModel have been called?
Forum Updated to NodeBB v4.3 + New Features

Updating QAbstractItemModel: Model is null although begin/endResetModel have been called?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 1.4k 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.
  • M Offline
    M Offline
    maxwell31
    wrote on last edited by maxwell31
    #1

    Hi,

    I have a QAbstractItemModel in c++ which I use in a TreeView. This is working fine. When the data changes I call a reset function:

    void MyTreeModel::reset(TreeBase* tb) {
        beginResetModel();
        setRootNode(tb);
        endResetModel();
    }
    

    The update works, but I get errors of the form

    qrc:/TreeViewScreen.qml:339: TypeError: Cannot read property 'FirstProperty' of null
    

    The error shows up 3 times for every entry. What am I missing? I thought that the begin/endResetModel would be enough to notify the view.

    raven-worxR 1 Reply Last reply
    0
    • M maxwell31

      Hi,

      I have a QAbstractItemModel in c++ which I use in a TreeView. This is working fine. When the data changes I call a reset function:

      void MyTreeModel::reset(TreeBase* tb) {
          beginResetModel();
          setRootNode(tb);
          endResetModel();
      }
      

      The update works, but I get errors of the form

      qrc:/TreeViewScreen.qml:339: TypeError: Cannot read property 'FirstProperty' of null
      

      The error shows up 3 times for every entry. What am I missing? I thought that the begin/endResetModel would be enough to notify the view.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @maxwell31 said in Updating QAbstractItemModel: Model is null although begin/endResetModel have been called?:

      The update works, but I get errors of the form
      qrc:/TreeViewScreen.qml:339: TypeError: Cannot read property 'FirstProperty' of null

      show some more (QML) code.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maxwell31
        wrote on last edited by
        #3

        My model is set as a contextProperty in c++:

        _myTreeModel = new MyTreeModel
        _engine->rootContext()->setContextProperty("treeModel", _myTreeModel);
        qmlRegisterUncreatableType<MyTreeModel>("MyTreeModel",1,0,"TreeModel", "problem treemodel");
        

        In the QML file I use it like this

        QC1.TreeView {
            id: treeView
            model: treeModel
            anchors.fill: parent
        
            QC1.TableViewColumn {
            width: 270
            id: namecolumn
            title: "Name"
            role: "Name"
        
            }
            QC1.TableViewColumn {
            id: namecolumn2
            title: "Value"
            role: "FirstProperty"
            delegate: myDelegate
            }
        }
        

        and the delegate is

        Component {
            id: myDelegate
            Loader { // loader choosing the right component
            property var firstProperty: model.FirstProperty
            property string typeString: model.Type
            property var thisIndex: styleData.index
            sourceComponent: if(typeString=='bool') {
                                checkBoxDelegate
                            }
                            else if(typeString=='double') {
                                floatDelegate
                            }
                            else if(typeString=='int32_t') {
                                floatDelegate
                            }
                            else { stringDelegate}
            }
        }
        Component {
            id: checkBoxDelegate
            CheckBox{text: ""
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 2
            checked: treeModel.data(thisIndex,TreeModel.FirstProperty)
            onCheckedChanged: {
                treeModel.setData(thisIndex, checked,TreeModel.FirstProperty)
            }
            }
        }
        

        as you see I load different delegates, depending on the type.

        raven-worxR 1 Reply Last reply
        0
        • M maxwell31

          My model is set as a contextProperty in c++:

          _myTreeModel = new MyTreeModel
          _engine->rootContext()->setContextProperty("treeModel", _myTreeModel);
          qmlRegisterUncreatableType<MyTreeModel>("MyTreeModel",1,0,"TreeModel", "problem treemodel");
          

          In the QML file I use it like this

          QC1.TreeView {
              id: treeView
              model: treeModel
              anchors.fill: parent
          
              QC1.TableViewColumn {
              width: 270
              id: namecolumn
              title: "Name"
              role: "Name"
          
              }
              QC1.TableViewColumn {
              id: namecolumn2
              title: "Value"
              role: "FirstProperty"
              delegate: myDelegate
              }
          }
          

          and the delegate is

          Component {
              id: myDelegate
              Loader { // loader choosing the right component
              property var firstProperty: model.FirstProperty
              property string typeString: model.Type
              property var thisIndex: styleData.index
              sourceComponent: if(typeString=='bool') {
                                  checkBoxDelegate
                              }
                              else if(typeString=='double') {
                                  floatDelegate
                              }
                              else if(typeString=='int32_t') {
                                  floatDelegate
                              }
                              else { stringDelegate}
              }
          }
          Component {
              id: checkBoxDelegate
              CheckBox{text: ""
              anchors.bottom: parent.bottom
              anchors.bottomMargin: 2
              checked: treeModel.data(thisIndex,TreeModel.FirstProperty)
              onCheckedChanged: {
                  treeModel.setData(thisIndex, checked,TreeModel.FirstProperty)
              }
              }
          }
          

          as you see I load different delegates, depending on the type.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @maxwell31
          see this example.
          It may be that only the delegate component itself gets the properties assigned

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maxwell31
            wrote on last edited by
            #5

            I fear I don't understand what you mean

            DiracsbracketD 1 Reply Last reply
            0
            • M maxwell31

              I fear I don't understand what you mean

              DiracsbracketD Offline
              DiracsbracketD Offline
              Diracsbracket
              wrote on last edited by Diracsbracket
              #6
              This post is deleted!
              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