Can't emit signal in QML custom Item

2019-09-07 13:06发布

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:

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条回答
Root(大扎)
2楼-- · 2019-09-07 13:57

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).

So just change this line:

onClicked:  baseButton.clicked()
查看更多
登录 后发表回答