Can't emit signal in QML custom Item

耗尽温柔 提交于 2019-12-12 06:19:15

问题


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:


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


来源:https://stackoverflow.com/questions/30108019/cant-emit-signal-in-qml-custom-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!