Exposing list of Qpoint from C++ to Qml
-
Hi,
I'm trying to expose a list of Qpoint from C++ to qml.
I've added this @ Q_PROPERTY(QVariantList tab READ tab)@ and this @
QVariantList TestList::tab() const
{
QVariantList list;
list.append(QPoint(5,12));
list.append(QPoint(6,0));
list.append(QPoint(45,2));
list.append(QPoint(9,17));
list.append(QPoint(3,15));return list;
}@
And in Qml side I'm trying that : @import QtQuick 1.0
Rectangle {
width: 100
height: 100
color: "red"TestList{ id:list } Component.onCompleted:{ console.log("leght:"+list.tab.length); for (var i=0; i<list.tab.length; i++) { console.log("Array item:", list.tab[i]," "+list.tab[i].x) } }
}@
But I can't access my QPoint data this way it said "undefined" for the ".x" part. And I don't know how to do it. Did I miss something ?
Thanks for reading me.
-
-
A model can be defined by subclassing QAbstractItemModel. This is the best approach if you have a more complex model that cannot be supported by the other approaches. A QAbstractItemModel can also automatically notify a QML view when the model data has changed.
-
Problem is QML is not understanding how to access QPoint::x() method. It is neither a slot nor declared as Q_INVOKABLE.
In order to do that create a QObject wrapper around QPoint and supply Q_INVOKABLE methods namely x(), y() which inturn returns the underlying QPoint x,y. Expose this as a QObjectList to qml. Things should work.
Also for your reference see "qml basic type point":http://doc.qt.nokia.com/latest/qml-point.html and
Qt.point() so that you can rethink your implementation.