Santhosh
Santhosh

Reputation: 637

QML - Access QList data through model

I was having an issue in trying to access C++ list data that is assigned to QML Repeater model.

Can this data be accessed without the help of a delegate?

For example: C++:

QList<MyOwnStruct>  GetListData() // Invokable from qml
{
   QList<MyOwnStruct> infoData
   .... // Appended data 
   return infoData
}

QML:

Row {
    Repeater {
        model: GetListData()
        ....
    }
}

In the above example, I was able to get the exact count of infoData which repeater repeats. But I could not get more information from infoData like each individual element.

Upvotes: 1

Views: 1063

Answers (1)

Ajith
Ajith

Reputation: 633

If you want to accesses elements of your struct, you will need to convert your MyOwnStruct to something that QML can understand, example QVariantMap.

So you could do something like this.

QVariantMap MyClass::GetData(int index) // Invokable
{
   QVariantMap var;

   MyOwnStruct infoData = infoListData[index];

   // Appended data 
    var.insert("elem_a", infoData.element_a);
    var.insert("elem_b", infoData.element_b);

    return var;
}

Then in QML, you can access is something like

var data_at_index = getData(index)
var a = data_at_index["elem_a"]

Your Repeater model can be just a Q_PROPERTY integer that returns the count of the list.

Upvotes: 1

Related Questions