【问题标题】:Communication between 2 different objects in 2 different QML files2 个不同 QML 文件中的 2 个不同对象之间的通信
【发布时间】:2020-01-29 08:06:06
【问题描述】:

我有一个 main.qml 文件和另一个“example.qml”文件。当我从“example.qml”文件中按下按钮时,我想更改“main.qml”文件中的文本。我尝试定义文本的来源。我试过发送信号。我尝试使用加载器,但总是走到了死胡同。

import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Imagine 2.12
import QtQuick.Window 2.0

//main.qml
Window {
       visible: true
       width: 1080
       height: 720
       color: "black"
       title: qsTr("MY GUI")

    Text {
       id: deneme
       x: 100
       y: 400
       color: "white"
       text: "Trial"

   }
} 
//example.qml

Item {
    id: difflock

    Rectangle{
        id: diffLockRect
        width: 1080
        height: 720
        color: "red"
        signal  mySignal

        Button{.


          onClicked: main.deneme.text = "Finally"
        }
    }
}

【问题讨论】:

    标签: qt qml qqmlcomponent


    【解决方案1】:
    • 创建一个以 Example.qml 命名的新 qml(首字母应为大写)
    • main.qml 中定义
    • Example.qml 可以访问 main.qml 中的对象

    ma​​in.qml

    import QtQuick 2.12
    import QtQuick.Layouts 1.12
    import QtQuick.Controls 2.12
    import QtQuick.Controls.Imagine 2.12
    import QtQuick.Window 2.0
        Window {
               visible: true
               width: 1080
               height: 720
               color: "black"
               title: qsTr("MY GUI")
    
               Example{id:rfrnc} // You can also reach the other qml objects by using this id
    
            Text {
               id: deneme
               x: 100
               y: 400
               color: "white"
               text: "Trial"
    
           }
        }
    

    Example.qml

    import QtQuick 2.12
    import QtQuick.Layouts 1.12
    import QtQuick.Controls 2.12
    import QtQuick.Controls.Imagine 2.12
    import QtQuick.Window 2.0
    
    Item {
        id: difflock
    
        Rectangle{
            id: diffLockRect
            width: 1080
            height: 720
            color: "red"
            signal  mySignal
    
            Button{
    
    onClicked: deneme.text = "Finally"
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多