DelegateModel, cant insert any object elements



  • Hello all,

    i want to filter my model by a property provided also per property. remoteworkstations is a QList<QObject*> with my self definded object. These objects are registered for qml and visible by the debugger. My problem is the items.insert(entry, "test"); has no effect, even if I delete the "if" statement before the insert. Need the model to inherit from abstractmodel or something? Where is my failure?

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQml.Models 2.2
    
    Item {
        width: parent.width
        height: parent.width
        id: root
        property var remoteworkstations
        property string projectname
    
        ListView {
            id: displayListView
            anchors.fill: parent
            spacing: 5
            model: displayDelegateModel
        }
    
        DelegateModel {
            id: displayDelegateModel
    
            delegate: MainSection{
                identifier: Hostname
            }
            model: remoteworkstations
            groups: [
                DelegateModelGroup {
                includeByDefault: false
                name: "test"
                }
            ]
            filterOnGroup: "test"
            Component.onCompleted: {
                var rowCount = remoteworkstations.length; //23
                items.remove(0,rowCount); //0
                for( var i = 0;i < rowCount;i++ ) {
                    var entry = root.remoteworkstations[i]; //Entry visible in the debugger
                    if(root.projectname === entry.AssignedProject.Projectname)
                        items.insert(entry,  "test"); //
                }
                console.log(items.count) //always 0
            }
        }
    
    }
    


  • @masterofeye said in DelegateModel, cant insert any object elements:

    remoteworkstations

    Try to build entry with dummy js-object.

    var entry = {
        prop1: "one",
        prop2: 2
    };
    

    Very important to make object corresponds other content of the model.
    The is from the doc:
    "Creates a new entry at index in a DelegateModel with the values from data that correspond to roles in the model assigned to DelegateModel::model."



  • Thx for answer but i got the same results.
    When I try following, before i delete the item list

     var entry2 =items.get(0)
    

    I should get the Item from the model. But the entry2 value hasn't any properties attached. I ecpect that i see the same results like in this row.

    var entry = root.remoteworkstations[i];
    


  • @masterofeye

    Doc about get()
    So, try

    var entry2 =items.get(0).model
    


  • I get what I want now, but the documentation for this not realy intuitive. I think there need's to be more examples, on how to set which property with which value.

    Component.onCompleted: {
                var rowCount = RemoteWorkstations.length;
                for( var i = 0;i < rowCount;i++ ) {
                    if( items.get(i).model.modelData.AssignedProject.Projectname === projectname) {
                         items.get(i).inTest = true
                    }
                }
            }
    

Log in to reply
 

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