DelegateModelGroup together with a C++ model



  • Hi,
    I'm looking for a way to show the content of a C++ model (QStandardItemModel) in a Qml ListView togehter with a DelegateModelGroup as filter. The Problem is the "get(int index)" function in DelegateModelGroup which delivers in normal a Javascript Object (unknown type) as return value.

    The C++ model consists of 3 roles (role1,...,role3). For illustration I created a small sample code.

    1.) C++ model

    Came::Came(QObject *parent) : QStandardItemModel(parent)
    {
        QStandardItem *it0 = new QStandardItem();
        it0->setData("flash", Came::role1);
        it0->setData("on", Came::role2);
        it0->setData(false, Came::role3);
        appendRow(it0);
    
        QStandardItem *it1 = new QStandardItem();
        it1->setData("flash", Came::role1);
        it1->setData("off", Came::role2);
        it1->setData(true, Came::role3);
        appendRow(it1);
    
        QStandardItem *it2 = new QStandardItem();
        it2->setData("flash", Came::role1);
        it2->setData("auto", Came::role2);
        it2->setData(false, Came::role3);
        appendRow(it2);
    }
    
    QVariantMap Came::get(const int rowNumber) const
    {
        QVariantMap map;
        QHash <int,QByteArray> roleName = roleNames();
        foreach (int i, roleName.keys())
        {
            map[roleName.value(i)] = data(index(rowNumber,0),i);
        }
        return map;
    }
    
    QHash<int, QByteArray> Came::roleNames() const
    {
         QHash<int, QByteArray> roles;
         roles[Came::role1] = "one";
         roles[Came::role2] = "two";
         roles[Came::role3] = "three";
         return roles;
    }
    
    void Came::updData(QModelIndex index, bool status)
    {
        setData(index,status,Came::role3);
    }
    

    2.) Qml ListView/DelegateModel

    Rectangle {
        id: iRoot
        width: parent.width
        height: parent.height
        color: "#aa000000"
        visible: mVis
    
        property bool mVis: false
    
        Rectangle {
            width: parent.width*0.6
            height: parent.height*0.6
            anchors.centerIn: parent
            color: "#343434"
            border.color: "#5F5F5F"
            radius: cRad*2
    
    
            DelegateModel {
                id: iDelModel
                model: Came
                property bool change: false
    
                delegate: Rectangle {
                    height: 25
                    width: 100
                    color: "transparent"
                    border.color: "white"
    
                    Item {
                        anchors.fill: parent
    
                        Text {
                            text: one + " : " + two + " : " + three
                        }
    
                        MouseArea {
                           anchors.fill: parent
                           onClicked: {
                               console.log(index);
                               Came.updData(iDelModel.modelIndex(index),true);
                           }
                       }
                    }
                }
    
                groups: [
                    DelegateModelGroup {
                        includeByDefault: false
                        name: "flash" }
                ]
    
                filterOnGroup: "flash"
    
                Component.onCompleted: {
                    var rowCount = Came.rowCount();
    
                    items.remove(0,rowCount);
                    for( var i = 0;i < rowCount;i++ ) {
                        var entry = Came.get(i); // Model in Qml -> Javascript Object; Model in C++ I tried with QVariantMap
                        console.log(entry);
                        if(entry["one"] == "flash") {
                            items.insert(i,entry, "flash");
                            console.log(items.get(i));
                        }
                    }
                }
            }
    
            ListView {
                id: iListView
                anchors.fill: parent
                model: iDelModel
                highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
                focus: true
            }
      }
    }
    

    Without DelegateModelGroup everything works fine. But with, the index of Delegate is always -1. Unfortunately the index is important to update the C++ model. Does someone know what I am doing wrong here? Or what is the normal way if I have one C++ model and want to show the content in different Qml ListViews regarding different groups?

    Thanks in advance...


Log in to reply
 

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