Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Treeview model properties are null in delegate warning

Tags:

c++

qt

qml

qtquick2

I have a TreeView

TreeView {
        id: dndView
        rowDelegate: Item {
            height: 30
        }
        itemDelegate:  dndDelegate
        model: myModel
        TableViewColumn {
            title: "Name"
            resizable: true
        }
}

and its delegate which does work

Rectangle {
    id: dragRect
    anchors.horizontalCenter: parent.horizontalCenter
    anchors.verticalCenter: parent.verticalCenter
    color: 'gray'
    width: dndView.width - 20
    height: 30
    Image {
        id: menuItemImage
        anchors.verticalCenter:parent.verticalCenter
        source:model.CommandIcon
    }
    Text {
        anchors.left:menuItemImage.right
        anchors.verticalCenter:parent.verticalCenter
        text:model.CommandTitle
        font.pixelSize: 14
    }

Roles in C++ file:

QHash<int, QByteArray> MenuTreeModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[TitleRole] = "CommandTitle";
    roles[IconRole] = "CommandIcon";
    return roles;
}

It displays text and icon correctly but nevertheless when I expand the items or close the application I have warnings:

qrc:/DraggableRectangle.qml:15: TypeError: Cannot read property 'CommandIcon' of null qrc:/DraggableRectangle.qml:20: TypeError: Cannot read property 'CommandTitle' of null*

What's wrong with it?

like image 670
amplifier Avatar asked Aug 08 '26 02:08

amplifier


1 Answers

If the warnings only appear on specific events, it may mean the model is destroyed or not yet created when your QML view tries to use it.

E.g. if the model is destroyed before the view when you close the application, the view still tries to use a destroyed object for a short time (before it's destroyed too).

Try this :

text: model ? model.CommandTitle : ""
like image 85
Ethyl Avatar answered Aug 10 '26 19:08

Ethyl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!