1

我有一个 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"
        }
    }
}

4

1 回答 1

0
  • 创建一个以Example.qml 命名的新 qml(第一个字母应为大写)
  • main.qml中定义
  • Example.qml可以访问main.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
    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"
        }
    }
}
于 2020-01-29T08:14:32.563 回答