我想更改另一个qml文件中存在的标签文本
text.qml:
import QtQuick 2.0
import QtQuick.Controls 2.3
Item {
Rectangle {
id: rectangle
x: 0
y: 0
width: 672
height: 480
color: "#ffffff"
GroupBox {
id: groupBox
x: 56
y: 155
width: 537
height: 285
title: qsTr("Group box")
Label {
id: labelname
x: 54
y: 27
text: qsTr("John")
}
Label {
id: labelsname2
x: 54
y: 62
text: qsTr("devid")
}
}
}
}
我想从main.qml更改labelname.text
我的main.qml是
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.3
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Button {
id: button
x: 190
y: 127
text: qsTr("change name")
onClicked: {
//i want to change text of labelname in text.qml
}
}
}
我想从cpp页面发送信号..signal包含人员名称,这些名称在text.qml文件中设置为labelname
答案 0 :(得分:1)
您可以设置属性alias以便从文件外部读取和写入元素的某个属性:
MyForm.qml:
import QtQuick 2.0
import QtQuick.Controls 2.3
Item {
property alias labelNameText: labelname.text
Rectangle {
id: rectangle
x: 0
y: 0
width: 672
height: 480
color: "#ffffff"
GroupBox {
id: groupBox
x: 56
y: 155
width: 537
height: 285
title: qsTr("Group box")
Label {
id: labelname
x: 54
y: 27
text: qsTr("John")
}
Label {
id: labelsname2
x: 54
y: 62
text: qsTr("devid")
}
}
}
}
以下是使用别名的方法:
main.qml:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.3
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
MyForm {
id: myForm
}
Button {
id: button
x: 190
y: 127
text: qsTr("change name")
onClicked: {
console.log('Name before change: ' + myForm.labelNameText);
myForm.labelNameText = 'Herbert';
}
}
}