小编Dea*_*eng的帖子

如何在 QML 中访问和更改组件中的项目

在 Qml 文件中,代码如下:

StackView {
    id: stackView
    anchors.right: parent.right
    width: parent.width/2-20
    initialItem:patientdetail

    Component{
        id:patientdetail
        Column {
            id:detailcolumn
            spacing: 2
            anchors.right: parent.right
            width: parent.width/2-20

            Label {
                id: label
                color: "#ffffff"
                text: qsTr("User ID")
            }

            TextField {
                id: textField_id
                readOnly: true
                placeholderText: qsTr("")
            }
        }
      }
    Component{
        id:component2
        //...other component will add to stackview
      }
   }
Run Code Online (Sandbox Code Playgroud)

我想通过 JS 函数(在同一个文件中)更改 TextField 的文本,例如:

function updatedetail(clear,rowindex){
    if(clear){
        textField_id.text="";
    }
    else{
        textField_id.text=jsonModel1.model.get(rowindex).id;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是有一个错误:

ReferenceError: textField_id 未定义

错误发生在哪里?

qt components qml

2
推荐指数
1
解决办法
3994
查看次数

标签 统计

components ×1

qml ×1

qt ×1