加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – QML – 如何将变量从一个qml文件发送/传递到其他qml文件

发布时间:2020-12-16 09:51:22 所属栏目:百科 来源:网络整理
导读:如何在qml文件之间发送变量或信号? http://i.stack.imgur.com/MChCG.png 主窗口 – 创建一个组件Item2.qml MainWindow – 创建一个组件item1.qml item1.qml – 创建一个组件Item3.qml Item3.qml – 更改/发送变量或信号到ItemII.qml(在主窗口中创建) – 如
如何在qml文件之间发送变量或信号?

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

>主窗口 – >创建一个组件Item2.qml
> MainWindow – >创建一个组件item1.qml
> item1.qml – >创建一个组件Item3.qml
> 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)
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读