【问题标题】:Can't emit signal in QML custom Item无法在 QML 自定义项中发出信号
【发布时间】:2015-05-07 17:33:00
【问题描述】:

我使用信号clicked 创建了我自己的项目,该项目包含 MouseArea。当单击 MouseArea 时,我想发出信号 clicked。但没有任何效果。 这是我的 .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
    }
}

非常感谢您的帮助!

【问题讨论】:

    标签: qt qml signals-slots


    【解决方案1】:

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

    所以只要改变这一行:

    onClicked:  baseButton.clicked()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 2012-06-24
      相关资源
      最近更新 更多