无法在QML自定义项中发出信号

时间:2021-03-14 08:12:17

I created my own Item with signal clicked, that contatins MouseArea. I want to emit signal clicked, when MouseArea is clicked. But nothing works. Here is my .qml code:

我创建了自己的项目,点击了信号,其中包含MouseArea。单击MouseArea时,我想点击发出的信号。但没有任何作用。这是我的.qml代码:

import QtQuick 2.4

Item {

    id: baseButton

    property alias text: txt.text
    width: txt.width
    height: txt.height

    signal clicked

    onClicked : console.log("Clicked!")

    Text {
        id: txt
        color: "white"
        font.pointSize: 8
        anchors.centerIn: parent
    }

    MouseArea {
        id: mousearea
        anchors.fill: parent
        hoverEnabled: true

        onEntered: {
            txt.color = "yellow"
            txt.font.pointSize = 15
        }

        onExited: {
            txt.color = "white"
            txt.font.pointSize = 8
        }

        onClicked:  baseButton.clicked
    }
}

I'll be very grateful for your help!

我将非常感谢你的帮助!

1 个解决方案

#1


Functions (which signals are) are first class objects in JS, so it is not an error to refer to them without parentheses. But you need them in order to execute the function (i.e. emit the signal).

函数(信号是)是JS中的第一类对象,因此在没有括号的情况下引用它们不是错误。但是你需要它们来执行功能(即发出信号)。

So just change this line:

所以只需改变这一行:

onClicked:  baseButton.clicked()

#1


Functions (which signals are) are first class objects in JS, so it is not an error to refer to them without parentheses. But you need them in order to execute the function (i.e. emit the signal).

函数(信号是)是JS中的第一类对象,因此在没有括号的情况下引用它们不是错误。但是你需要它们来执行功能(即发出信号)。

So just change this line:

所以只需改变这一行:

onClicked:  baseButton.clicked()