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

Qt 如何从QML组件调用自定义信号?,qt,qml,loader,Qt,Qml,Loader,有没有一种方法可以从其他地方加载的组件中包含的mouseArea调用信号 单击矩形时,不会输入下面示例中的onclick 结构需要保持定义不变。能够定义将阴影应用于任何源的qml组件 代码如下: Window{ visible: true width: 640 height: 480 title: qsTr("Hello World") Item { id: mainRectangle anchors.center

有没有一种方法可以从其他地方加载的组件中包含的mouseArea调用信号

单击矩形时,不会输入下面示例中的onclick

结构需要保持定义不变。能够定义将阴影应用于任何源的qml组件

代码如下:

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
                }
            }
        }
    }
}
最后,鼠标应该显示它现在的功能,鼠标earea应该可以工作

单击矩形时,不会输入下面示例中的onclick

mainRectangle
不可见,不可见的项不会获取输入事件。由于
MouseArea
mainRectangle
的子对象,因此它也不会获取事件

最后,鼠标应该显示它现在的功能,鼠标earea应该可以工作

您可以直接在
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
                }
            }
        }
    }
}

我已经编辑了代码。还有一个阴影需要应用,如果我不使用着色器设置不透明度,颜色将混合(阴影将在加载的组件后面可见)。请提供实际运行的代码。在您的情况下,向根项目添加一个要触发的信号,
rect
。例如,
signal mySignal()
并从您需要的
root.mySignal()
位置调用它,只要
MouseArea
不可见,因此无法获取事件,它就无法工作。已启用layer.effect和layer.effect@Mitch您是否愿意添加解决方案,因为您指出了它。谢谢:)好的,如果我做对了就告诉我。我没有效果,所以答案没有用。