简体   繁体   English

Unexpected Token `:' 同时定义 QML 信号参数类型

[英]Unexpected Token `:' while defining QML signal parameter type

I am trying to familiarize myself with QML.我正在尝试熟悉 QML。 I am following this Qt provide tutorial, but have run into a problem.我正在关注Qt 提供的教程,但遇到了问题。

Problem:问题:

While creating the Cell.qml file, I am required to create a clicked signal, where the single parameter named cellColor is defined as a color type ( if I understand this correctly )在创建Cell.qml文件时,我需要创建一个点击信号,其中名为cellColor的单个参数被定义为color类型(如果我理解正确的话)

../QMLTest/Cell.qml:6 Unexpected token `:' ../QMLTest/Cell.qml:6 意外令牌“:”

The code snippet in question from Cell.qml is:来自Cell.qml的代码片段是:

import QtQuick 2.0

Item {
    id: container
    property alias cellColor: rectangle.color
    signal clicked(cellColor: color)                   <-----Problem - line 6

    width: 40; height: 25
    //...
}

Since QML uses Javascript, I figured I'll try this, which worked:由于 QML 使用 Javascript,我想我会试试这个,它有效:

signal clicked(var cellColor)

One obvious drawback is you lose the defined type (based on my assumption earlier).一个明显的缺点是您丢失了定义的类型(基于我之前的假设)。

Question:问题:

Why did the provided signal code not work, and is there any drawback to using the var keyword instead of defining the type?为什么提供的信号代码不起作用,使用var关键字而不是定义类型有什么缺点吗?

Update更新

Added after answer was accepted.接受答案后添加。

I should also have mentioned, I am using Qt Creator 4.10.0 with Qt 5.13.1 (MSVC 32bit)我还应该提到,我正在使用 Qt Creator 4.10.0 和 Qt 5.13.1(MSVC 32 位)

This syntax is new in Qt 5.14.此语法是 Qt 5.14 中的新语法。 It's following TypeScript syntax and the way to go for the upcoming QML 3. With older Qt versions, one gets the error message that you mentioned. It's following TypeScript syntax and the way to go for the upcoming QML 3. With older Qt versions, one gets the error message that you mentioned. The classical and so far still supported syntax would be经典且迄今为止仍受支持的语法是

signal clicked(color cellColor)

The name: type syntax isn't even mentioned yet in the Qt documentation about QML signals .关于 QML 信号的 Qt 文档中甚至没有提到name: type语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM