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



  • 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.


  • Moderators

    @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.



  • 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.


  • Moderators

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



  • I fear I don't understand what you mean



  • This post is deleted!

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.