【发布时间】:2020-10-28 17:10:21
【问题描述】:
当我尝试打印 Val1 和 Val2 的值时,我正在使用自定义 inputRow 和 MenuButton 放置在组件外部,它给出“ReferenceError: val1 is not defined”, 我如何在组件外部访问它。
InputRow {
name:"Command"
enabled: true
filename: "qrc:/AutonomyPlatform/Images/Parameters.svg"
component: Column {
spacing: 10
Row{
spacing: 50
Text {
text: "Val1"
color: Colors.menuBodyTextNormal
font.pointSize: 10
}
TextInput {
id:val1
width: 5
text: selectedModel ? "0" : ""
font.pointSize: 10
color: Colors.menuBodyTextInput
}
Text {
text: "m"
color: Colors.menuBodyTextNormal
font.pointSize: 10
}
}
Row {
spacing: 50
Text{
text: "Val2"
color: Colors.menuBodyTextNormal
font.pointSize: 10
}
TextInput {
id:val2
width: 5
text: selectedModel ? "0" : ""
font.pointSize: 10
color: Colors.menuBodyTextInput
}
Text {
text: "m"
color: Colors.menuBodyTextNormal
font.pointSize: 10
}
}
} //End of Column
MenuButton {
label: "Send"
buttonHeight: 25
buttonWidth: 35
onMenuButtonClicked:
{
console.log("VAL1",val1.text) //ReferenceError: val1 is not defined,
console.log("VAL2",val2.text)
console.log("SEND")
}
}
}
当我将 Menubutton 放在列组件中时,它会按预期打印,但是当它的外部组件出现上面提到的 ReferenceError 时
【问题讨论】:
-
这是因为组件是不同的变量作用域,因为它会在其他地方创建(可能是Loader,但我不清楚)
-
如果你使用像
Loader这样的延迟加载,则范围仅限于组件边界,与此无关。但由于 QML 是声明性语言,您必须以声明性方式构建应用程序逻辑。
标签: qt qml qqmlcomponent qqmllistproperty