简体   繁体   English

如何从QML组件调用自定义信号?

[英]How can I call a custom signal from a QML Component?

Is there a way to call a signal from a mouseArea included in a component which is loaded somewhere else? 有没有一种方法可以从其他位置加载的组件中包含的mouseArea调用信号?

onClicked in the example below is not entered when i click on the rectangle. 当我单击矩形时,未输入以下示例中的onClicked。

The structure needs to remain as defined. 该结构需要保持定义。 To be able to define a qml component that applies a shadow to any source 为了能够定义将阴影应用于任何源的qml组件

Here is the code: 这是代码:

Window{
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Item
    {
        id: mainRectangle
        anchors.centerIn: parent
        width: loaderId.width + 60
        height: loaderId.height + 60

        Rectangle {
            id: rect2
            anchors.right: mainRectangle.right
            anchors.top: mainRectangle.top
            anchors.rightMargin: -30
            anchors.topMargin: -30
            width: 100
            height: 100
            color: "red"
            opacity: 0.5
        }

        Loader {
            id: loaderId
            anchors.centerIn: parent
            sourceComponent: component
            active:true
        }
        visible: false
    }

    ShaderEffectSource {
        id: shader
        anchors.fill: mainRectangle
        anchors.margins: -30
        sourceItem: mainRectangle
        opacity: 0.5
        visible: true
    }

    Component {
        id: component
        Rectangle {
            id: rect
            width: 100
            height: 100
            color: "black"
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    console.log("Clicked!")
                    // call a signal from here
                }
            }
        }
    }
}

In the end the should show what it does now and the mouseArea should work. 最后,应该显示它现在正在做什么,并且mouseArea应该工作。

onClicked in the example below is not entered when i click on the rectangle. 当我单击矩形时,未输入以下示例中的onClicked。

mainRectangle is not visible, and items that aren't visible don't get input events. mainRectangle不可见,不可见的项目也不会获得输入事件。 Since the MouseArea is a child of mainRectangle , it won't get events either. 由于MouseAreamainRectangle的子mainRectangle ,因此它也不会获取事件。

In the end the should show what it does now and the mouseArea should work. 最后,应该显示它现在正在做什么,并且mouseArea应该工作。

Instead of hiding the source item and using ShaderEffectSource , you can set the opacity directly on mainRectangle and use item layers (the link has a similar example) to ensure that there is no overlap due to transparency: 您可以直接在mainRectangle上设置opacity并使用项目层 (链接具有类似的示例)来确保不存在由于重叠而导致的重叠,而不是隐藏源项目并使用ShaderEffectSource ,而不是:

import QtQuick 2.0
import QtQuick.Window 2.0

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Item {
        id: mainRectangle
        anchors.centerIn: parent
        width: loaderId.width + 60
        height: loaderId.height + 60
        opacity: 0.5
        layer.enabled: true

        Rectangle {
            id: rect2
            anchors.right: mainRectangle.right
            anchors.top: mainRectangle.top
            anchors.rightMargin: -30
            anchors.topMargin: -30
            width: 100
            height: 100
            color: "red"
        }

        Loader {
            id: loaderId
            anchors.centerIn: parent
            sourceComponent: component
            active: true
        }
    }

    Component {
        id: component
        Rectangle {
            id: rect
            width: 100
            height: 100
            color: "black"
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    console.log("Clicked!")
                    // call a signal from here
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM