LINUX.ORG.RU

История изменений

Исправление Liferer, (текущая версия) :

Вот пример решения на сигналах (за лишние куски и мусорность прошу не пинать - делал на основе примера из QtCreator):

import QtQuick 2.9
import QtQuick.Controls 2.2

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

    Connections {
        target: ldrr.item
        onMessage: ldr.item.text = "Tested"
    }

    Rectangle {
        id: stackView        
        anchors.fill: parent

        Loader {
            id: ldr
            sourceComponent: comp
        }
        Loader {
            id: ldrr
            anchors.top: ldr.bottom
            sourceComponent: compp
        }

        Component {
            id: comp
            Rectangle {
                property alias text: lab.text
                id: recttt
                width: 50
                height: 50
                anchors.left: window.left
                color: "red"
                Label {
                    id: lab
                    text: "default"
                }
            }
        }
        Component {            
            id: compp
            Rectangle {                
                id: rect
                signal message(string str)
                width: 50
                height: 50
                color: "blue"
                anchors.right: window.right
                MouseArea {
                    anchors.fill: parent
                    onClicked: message("Test")
                }
            }
        }
    }
}

Нужно понимать, что такое Component - грубо говоря, инлайновый аналог QML файла, а файл сам по себе не может содержать проперти или сигналы, поскольку не является объектом.

Исходная версия Liferer, :

Вот пример решения на сигналах (за лишние куски и мусорность прошу не пинать - делал на основе примера из QtCreator):

import QtQuick 2.9
import QtQuick.Controls 2.2

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

    Connections {
        target: ldrr.item
        onMessage: ldr.item.text = "Tested"
    }

    Rectangle {
        id: stackView        
        anchors.fill: parent

        Loader {
            id: ldr
            sourceComponent: comp
        }
        Loader {
            id: ldrr
            anchors.top: ldr.bottom
            sourceComponent: compp
        }

        Component {
            id: comp
            Rectangle {
                property alias text: lab.text
                id: recttt
                width: 50
                height: 50
                anchors.left: window.left
                color: "red"
                Label {
                    id: lab
                    text: "default"
                }
            }
        }
        Component {            
            id: compp
            Rectangle {                
                id: rect
                signal message(string str)
                width: 50
                height: 50
                color: "blue"
                anchors.right: window.right
                MouseArea {
                    anchors.fill: parent
                    onClicked: message("Test")
                }
            }
        }
    }
}