如何在Stackview QML中收听来自initalItem的信号

时间:2018-10-28 18:35:21

标签: qt qml qt5

我想听QML initialItemStackView发出的信号。而且看来我认为如何做错了。

StackView {
    id: stackView
    initialItem:{
            myHomeForm{
               onMySignal{
                myArray=signalArray
               }
            }
    }
    anchors.fill: parent
}

该文档仅说明如何设置属性,而没有说明如何监听信号 请非常感谢您

1 个答案:

答案 0 :(得分:1)

您可以在Component.onCompletedconnect之间建立连接:

import QtQuick 2.9
import QtQuick.Controls 2.2

ApplicationWindow {
    id: window
    visible: true
    width: 640
    height: 480
    title: qsTr("Stack")

    StackView {
        id: stackView
        initialItem: Page {
            id: page
            anchors.fill: parent
            signal mySignal()
            Button {
                text: qsTr("Click me")
                anchors.centerIn: parent
                onClicked: page.mySignal()
            }
        }
        anchors.fill: parent
        Component.onCompleted: initialItem.mySignal.connect(onMySignal)
        function onMySignal(){
            console.log("onMySignal")
        }
    }
}