PDA

View Full Version : How to pass a QML ListModel to C++ function



cmessineo
18th December 2013, 19:11
I have a ListModel in QML:



ListModel{
id: data
ListElement
{
x: 10
y: 20
count: 18
}
}

function draw(){
ctx.drawIntensityMap(data);
}


//********** c++ code

void Context2D::drawIntensityMap(QVariantList data)
{
....
}


The model just gets populated with x,y,count data via javascript. After it gets populated I want to pass it to my c++ function so I can draw a HeatMap with QPainter.
I can't figure out how to pass the data to my C++ function.

Would I have to pass it as a QVariantList? If so how would I get to the data in C++.

Thanks

wysota
18th December 2013, 23:43
Usually you'd do it the other way -- expose a C++ model that can be populated from QML and then use it from C++.

lip
29th May 2015, 10:52
Same question here. Anyone knows or is it possible?


Usually you'd do it the other way -- expose a C++ model that can be populated from QML and then use it from C++.:(:(:( Not an answer

anda_skoa
29th May 2015, 11:04
Well, as wysota said, it is easier on the C++ side to work with concrete types, but if you are content working with the common model interface then that is of course possible as well.

The ListModel is likely (could be checked in the sources) implemented as a QAbstractListModel subclass.
So passing the model as a QObject* and casting to that class should work.

Cheers,
_