I have written a QT Quick program use TabView. When I click the botton b1 which is in Tabview, the program should call show_text() and print the text of b1, but it print "ReferenceError: b1 is not defined". Any suggestion will be appreciated, thanks.
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.1
ApplicationWindow {
function show_text() {
console.log(b1.text)
}
TabView {
id: tv
Tab {
id: tab1
Button{
id: b1
text:"b1's text"
onClicked: {
//console.log(b1.text)
show_text()
}
}
}
}
}
Pass the object as a parameter
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.1
ApplicationWindow {
function show_text(myobject) {
console.log(myobject.text)
}
TabView {
id: tv
Tab {
id: tab1
Button{
id: b1
text: "b1's text"
onClicked: {
show_text(b1)
}
}
}
}
}
You can access in your object with this example.
ApplicationWindow {
function show_text() {
console.log(tv.b1Text);
}
TabView {
id: tv
property alias b1Text: b1.text
Tab {
id: tab1
Button{
id: b1
text:"b1's text"
onClicked: {
//console.log(b1.text)
show_text()
}
}
}
}
}
来源:https://stackoverflow.com/questions/21927587/referenceerror-in-qt-quick-controls-tabview