将QML编译为C++:注释JavaScript函数

Compiling QML to C++: Annotating JavaScript functions

Thursday May 19, 2022 by Ulf Hermann | Comments

​2022年5月19日,星期四,乌尔夫·赫尔曼 评论

This is the second installment in the series on how to adjust your QML application to take the maximum advantage of qmlsc. In the first post we've set up the environment and taken an initial measurement. I highly recommend reading that one first. What we've measured was the time the updateLockButton() function in ButtonsBar.qml takes before we optimize it. It was 61.4µs for an aggregation of 8 calls, or 7.67µs for each single call on average (which is not statistically significant, but good enough for demonstration purposes).

​这是关于如何调整QML应用程序以最大限度地利用qmlsc的系列文章的第二部分。在第一篇文章中,我们设置了环境并进行了初步测量。我强烈建议你先读这本书。我们测量的是ButtonsBar.qml中updateLockButton()函数的时间。需要在我们优化它之前进行。8次呼叫的聚合时间为61.4µs,平均每个呼叫时间为7.67µs(这在统计上并不显著,但足以用于演示)。

Now, let's go back to the warning we've seen. What is qmlsc actually complaining about? It doesn't find a type annotation on the "updateLockButton" function. This means it doesn't know of what type the "locked" parameter to the function is. Therefore, it cannot compile the function to C++. Well, this is easy to fix. "locked" is obviously a boolean, and we can state this:

现在,让我们回到我们看到的警告。qmlsc到底在抱怨什么?在“updateLockButton”函数上找不到类型注释。这意味着它不知道函数的“锁定”参数是什么类型。因此,它无法将函数编译为C++。嗯,这很容易修复。“locked”显然是一个布尔值,我们可以这样说:

function updateLockButton(locked: bool) {
    lockButton.checked = !locked;
}

Let's make this change and compile and profile again the same way. Notice that the compiler warning has disappeared. Did it make a difference? On my machine the statistics now show a cumulative time of 30.1µs, with each call taking 3.76µs on average.

让我们进行此更改,并以相同的方式再次编译和分析。请注意,编译器警告已消失。这有什么不同吗?在我的机器上,统计数据显示累计时间为30.1µs,每次通话平均耗时3.76µs。

Again, this is not a statistically significant measurement, but it shows how you can get a feeling for the impact of your changes.

同样,这不是一个统计上有意义的测量,但它显示了你如何感受到你的变化所带来的影响。

You see a similar warning for the "logButtonChecked()" function:

对于“logButtonChecked()”函数,您会看到类似的警告:

Warning: ButtonsBar.qml:46:27: Could not compile function lockButtonChecked: function without type annotation returns bool of QQuickAbstractButton::checked with type bool
        return lockButton.checked;

This means the compiler wants to see a return type annotation. The function obviously returns a boolean, so this is also easy to fix:

这意味着编译器希望看到返回类型注释。该函数显然返回布尔值,因此这也很容易修复:

function lockButtonChecked(): bool {
    return lockButton.checked;
}

The types you can use as annotation are QML types. They are distinct from JavaScript types. In particular, you can use value types and object types. You can explicitly specify "void" as return type in order to mark a function without arguments or return value as typed. The documentation for value types has recently been updated. You may need to look at the documentation snapshot for the latest version.

​可以用作注释的类型是QML类型。它们不同于JavaScript类型。特别是,可以使用值类型和对象类型。您可以显式指定“void”作为返回类型,以便将没有参数的函数或返回值标记为类型化。值类型的文档最近已更新。您可能需要查看最新版本的文档快照。

So, for example, you can have "int", "real", "vector2d", or "Rectangle" as type annotation, but not "object" or "number". "string" is a QML type as well as a JavaScript type.

例如,可以使用“int”、“real”、“vector2d”或“Rectangle”作为类型注释,但不能使用“object”或“number”。“string”是QML类型和JavaScript类型。

A description of type annotations from the perspective of invoking the functions from C++ is also available in the documentation.

​文档中还提供了从调用C++函数的角度对类型注释的描述。

Compatibility

兼容性

The fundamental concept of type annotations has been part of the language since Qt 5.14. "void" has been a builtin QML type since Qt 6.2.3.

 自Qt 5.14以来,类型注释的基本概念一直是该语言的一部分。自Qt 6.2.3以来,“void”一直是内置的QML类型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值