簡體   English   中英

QML - 如何將變量從一個qml文件發送/傳遞到其他qml文件

[英]QML - How to send/pass variable from one qml file to other qml file

如何在qml文件之間發送變量或信號?

http://i.stack.imgur.com/MChCG.png

  1. Mainwindow - >創建一個組件Item2.qml
  2. MainWindow - >創建一個組件item1.qml
  3. item1.qml - >創建一個組件Item3.qml
  4. Item3.qml - >更改/發送變量或信號到ItemII.qml(在主窗口中創建) - 如何?

有人可以寫一個小例子嗎?

示例代碼:

Item1.qml

//Item 1
import QtQuick 2.1

Rectangle {
    width: 200
    height: 100
    color:"red"
    Text{
        anchors.centerIn: parent
        font.pixelSize: 16
        text:"Item1"
        width:parent.width
        horizontalAlignment: Text.AlignRight
        color:"white"
    }
}

Item2.qml

//Item 2
import QtQuick 2.1

Rectangle {
    width: 100
    height: 100
    color:"blue"        
    Text{
        anchors.centerIn: parent
        font.pixelSize: 16
        text:"Item2"
        color:"white"
    }
}

Item3.qml

//Item 3
import QtQuick 2.1

Rectangle {
    id:item3
    width: item3Area.pressed?90:100
    height: item3Area.pressed?90:100
    color:"green"
    signal superAwesomeSignal(string txt)
    Text{
        anchors.centerIn: parent
        font.pixelSize: 16
        text:"Item3"
        color:"white"
    }
    MouseArea{
        id:item3Area
        anchors.fill: parent
        onClicked:item3.superAwesomeSignal("Hello, from Item 3 ")
    }
}

Main.qml

//Main.qml
import QtQuick 2.0

Rectangle {
    width: 360
    height: 360
    Item1{
        anchors.top: parent.top
        anchors.left: parent.left
        Item3{
            id:item3
            anchors.top: parent.top
            onSuperAwesomeSignal: item2.item3SignalReceived(txt)
        }
    }
    Item2{
        id:item2
        anchors.bottom: parent.bottom
        signal item3SignalReceived(string txt)
        onItem3SignalReceived:console.debug(txt)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM