gpt4 book ai didi

c++ - 如何加快对从 C++ 公开到 QML 的 QList 的访问

转载 作者:太空狗 更新时间:2023-10-29 21:22:59 26 4
gpt4 key购买 nike

我在访问 QList<qreal> 时遇到一些问题属性(property)。我已经声明:

Q_PROPERTY(QList<qreal> circlePointsX READ circlePointsX NOTIFY circlePointsXChanged);
QList<qreal> circlePointsX(void) const
{
return mCirclePointsX;
}

在 QML 文件中,我做了

pArea.circlesPointsX = paintAreaHelper.circlePointsX;

然后一些代码逐点读取:

    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]);
}

当然属性的类型是var

property var circlesPointsX;@

和分配:

var cPointsX = circlesPointsX;

不会加快任何速度,因为它只是复制引用。

我对其进行了调试,对于每个循环访问,都会调用 c++ 方法。我想从 c++ 复制数据一次,然后从“本地 qml 拷贝”访问它,而不是每次都调用 c++ getter。

最佳答案

documentation阐明了它:

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.

因此,您的解决方案是将 circlePointsX 声明为:

Q_INVOKABLE QList<qreal> circlePointsX() const { return mCirclePointsX; }

您应该删除 circlePoints 属性,或将其重命名为其他名称。

吹毛求疵:将 void 放在参数列表中是一种 C 主义,在 C++ 中没有立足之地。在 C 中使用它的原因是 void foo() 等同于 void foo(...)。在 C++ 中不再是这种情况。

关于c++ - 如何加快对从 C++ 公开到 QML 的 QList<qreal> 的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19365563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com