创建信号并使用该信号时无法分配给不存在的 属性

Cannot assign to non-existent property when creating signal and using that

我想创建一个信号,所以我有一个名为 TestCreateSignal.qml 的 qml 文件 内容如下:

import QtQuick 2.0
Rectangle
{
    color: "red"
    width: 344
    height: 233
    signal sendMessage
    MouseArea
    {
        onClicked:
        {
            sendMessage();
        }
    }
}

我想在另一个名为 TestUseSignal.qml

的 qml 文件中使用这个信号
import QtQuick 2.0
Rectangle
{
    TestCreateSignal
    {
        sendMessage: //Error is at this line
        {
            console.log("message sendded");
        }
    }
}

但是当我想使用它时出现这样的错误

qrc:/TestUseSignal.qml:5 Cannot assign to non-existent property "sendMessage"

根据 docs:

An object can be notified through a signal handler whenever a particular signal is emitted. A signal handler is declared with the syntax on<Signal> where <Signal> is the name of the signal, with the first letter capitalized. The signal handler must be declared within the definition of the object that emits the signal, and the handler should contain the block of JavaScript code to be executed when the signal handler is invoked.

你的情况:

TestCreateSignal
{
    onSendMessage:
    {
        console.log("message sendded");
    }
}