I have some problem with speed to access to QList<qreal> property.
I have declared:
Q_PROPERTY(QList<qreal> circlePointsX READ circlePointsX NOTIFY circlePointsXChanged);
QList<qreal> circlePointsX(void) const
{
return mCirclePointsX;
}
and in QML file, I made
pArea.circlesPointsX = paintAreaHelper.circlePointsX;
and after that some code is reading point by point:
var cPointsX = circlesPointsX;
var cPointsY = circlesPointsY;
var noOfPoints = circlesPointsX.length - 4;
for (var i = 0; i <= noOfPoints; i+=4)
{
ctx.moveTo(cPointsX[i], cPointsY[i]);
ctx.lineTo(cPointsX[i+1], cPointsY[i+1]);
ctx.lineTo(cPointsX[i+2], cPointsY[i+2]);
ctx.lineTo(cPointsX[i+3], cPointsY[i+3]);
ctx.lineTo(cPointsX[i], cPointsY[i]);
}
of course the type of property is var
property var circlesPointsX;@
and assignment:
var cPointsX = circlesPointsX;
does not speed up anything, because it's just copying the reference.
I debuged it, and for every single loop access, the c++ method is called. I would like to copy the data from c++ once and access it from "local qml copy" instead of calling c++ getter every time.
The documentation sheds some light on it:
If the sequence is exposed as a Q_PROPERTY, accessing any value in the sequence by index will cause the sequence data to be read from the QObject's property, then a read to occur. Similarly, modifying any value in the sequence will cause the sequence data to be read, and then the modification will be performed and the modified sequence will be written back to the QObject's property.
If the sequence is returned from a Q_INVOKABLE function, access and mutation is much cheaper, as no QObject property read or write occurs; instead, the C++ sequence data is accessed and modified directly.
So, your solution is to declare circlePointsX as:
Q_INVOKABLE QList<qreal> circlePointsX() const { return mCirclePointsX; }
You should drop the circlePoints property, or rename it to something else.
Nitpick: Putting void in the parameter list is a C-ism that has no place in C++. The reason for it in C was that void foo() is equivalent to void foo(...). This is no longer the case in C++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With