QML信号是否受其层次结构的约束?请参阅装载程序示例

时间:2014-03-11 08:35:43

标签: android qt

我有一个main.qml文件,它加载在名为ImageRed.qml和ImageBlue.qml的单独组件文件中定义的两个组件矩形。红色和蓝色组件都有一个按钮,可以提升Loader应该响应的相同信号。 Loader用名为ImageGreen的第三个组件替换了ImageBlue矩形。问题是加载器只响应ImageBlue上的按钮而不是ImageRed上的按钮引发的相同信号。为什么?

// main.qml

import QtQuick 2.2
import QtQuick.Controls 1.1

Item {
    id: imageselector
    width: 360
    height: 500
    Rectangle {
        id: background
        width: parent.width
        height: parent.height
        color: "lightblue"
        LoaderSwitch {}
        ImageRed {}
    }
}

// LoaderSwitch.qml

import QtQuick 2.2
import QtQuick.Controls 1.1

Item {
    id: loaderswitch
    width: parent.width
    height: parent.height
    property string pageswitch

    Loader { 
        id: switchBoardLoader
        focus: true
        anchors.fill: parent
        source:"ImageBlue.qml"
        signal boardClick (string pageswitch)
        onBoardClick: {
                console.log("..method: onBoardClick..")
            if (pageswitch === "green") {
                console.log("onBoardClick = ", pageswitch)
                switchBoardLoader.source = "ImageRed.qml"}}
        onLoaded: {
            switchBoardLoader.item.showState1.connect(boardClick)}
    }
}

// ImageBlue.qml

import QtQuick 2.2
import QtQuick.Controls 1.1
Item {
    id: blueItem
    width: 360
    height: 500
    property string stateSelected
    signal showState1(string stateSelected)
    Rectangle {color: "blue"; anchors.fill: parent
        Button {
            id: buttonBlue
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
            text:"Green"
            onClicked: {
                stateSelected = "green"
                showState1(stateSelected)
                console.log("Blue page = ",stateSelected)}}

    }
 }

// ImageGreen.qml

import QtQuick 2.2
import QtQuick.Controls 1.1

Item {
    id: greenItem
    width: 360
    height: 200
    property string stateSelected
    signal showState1(string stateSelected)
    Rectangle {color: "green"; anchors.fill: parent
        Button {
            id: buttonGreen
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
            text:"Green"}
    }
 }

// ImageRed.qml

import QtQuick 2.2
import QtQuick.Controls 1.1

Item {
    id: redItem
    width: 360
    height: 200
    property string stateSelected
    signal showState1(string stateSelected)
    Rectangle {color: "red"; anchors.fill: parent
        Button {
            id: buttonRed
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter
                text:"Green"
                onClicked: {
                    stateSelected = "green"
                    showState1(stateSelected)
                    console.log("Red page = ",stateSelected)}}
    }
 }

1 个答案:

答案 0 :(得分:0)

我设法通过将具有Loader要响应的信号的组件放入同一链中来解决Loader没有看到来自特定组件的信号的问题。例如

Chain 1 -| Loader (on Component A)
                |---- Signal (from Component B)
                |---- Signal (from Component C)
                        | -----Signal (from Component D)

Loader会看到所有这些信号,因为它们来自链中的孩子。但是,将不会看到来自不在Loader链中的组件的信号。

Chain 1 - | Loader (on Component A)
                 |---- Signal (from Component B)

Chain 2 - | Signal (from Component E)
                 | ---- Signal (from Component F)

A上的Loader不会看到组件E和F的信号。