PDA

View Full Version : Changing the text of a button



franky
25th December 2017, 19:37
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()
}
}
}


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?

d_stranz
28th December 2017, 19:45
Maybe try moving the definition of your function change() to -after- the declaration of Button. I don't know much QML, but it may require that ids must be defined before they can be used. st_text isn't defined until after Button is declared.

If that isn't it, then maybe st_text is ambiguous, and you need to qualify it as "b1.st_text".