Hi,
Using this code in a QML program I want to change the text of a button when clicked:
main.qml:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
Window {
visible: true
width: 720; height: 720
color: "gray"
Rectangle {
id: root
x: 10; y: 10
width: 300; height: 300
color: "lightblue"
function change() {
st_text.text = "Clicked"
}
Button {
id: b1
x: 100; y: 100
style: ButtonStyle {
background: Rectangle {
implicitHeight: 50
implicitWidth: 100
color: "lightGrey"
border.width: 4
border.color: "lightBlue"
radius: 15
Text {
id: st_text
anchors.centerIn: parent
text: "Start"
font.bold: true
font.pixelSize: 15
color: "green"
}
}
}
onClicked: root.change()
}
}
}
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
Window {
visible: true
width: 720; height: 720
color: "gray"
Rectangle {
id: root
x: 10; y: 10
width: 300; height: 300
color: "lightblue"
function change() {
st_text.text = "Clicked"
}
Button {
id: b1
x: 100; y: 100
style: ButtonStyle {
background: Rectangle {
implicitHeight: 50
implicitWidth: 100
color: "lightGrey"
border.width: 4
border.color: "lightBlue"
radius: 15
Text {
id: st_text
anchors.centerIn: parent
text: "Start"
font.bold: true
font.pixelSize: 15
color: "green"
}
}
}
onClicked: root.change()
}
}
}
To copy to clipboard, switch view to plain text mode
But when I click the button I get this error:
qrc:/main.qml:18: ReferenceError: st_text is not defined
Why and how to solve that please?
Bookmarks