简体   繁体   English

qml 中的自定义组件

[英]Custom Component in qml

I am Using custom inputRow and MenuButton is placed Outside component when i try to print value of Val1 and Val2 it gives "ReferenceError: val1 is not defined", how can i access it Outside component.当我尝试打印 Val1 和 Val2 的值时,我正在使用自定义 inputRow 和 MenuButton 放置在组件外部,它给出“ReferenceError:val1 未定义”,我如何访问它外部组件。

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")
            
        }
    }
}

When i put Menubutton inside column component it prints as expected but when its outside component i get ReferenceError as mentioned above当我将 Menubutton 放在列组件内时,它按预期打印,但是当它的外部组件时,我得到 ReferenceError 如上所述

As I mentioned above that could be done in a declarative style: For example:正如我上面提到的,可以以声明式的方式完成:例如:

Window {
    id: window
    visible: true
    width: 400
    height: 600
    title: qsTr("Test")

    property string str1: ""
    property string str2: ""

    onStr1Changed: printOut();
    onStr2Changed: printOut();

    function printOut()
    {
        console.log(str1, str2);
    }

    ColumnLayout {
        anchors.centerIn: parent
        Loader {
            id: loader1
            sourceComponent: TextInput {
                id: txt1
                text: "xxx"
                Binding {
                    target: window
                    property: "str1"
                    value: txt1.text
                }
            }
        }

        Loader {
            id: loader2
            sourceComponent: TextInput {
                id: txt2
                text: "xxx"
                Binding {
                    target: window
                    property: "str2"
                    value: txt2.text
                }
            }
        }
    }

    Component.onCompleted: {
        loader1.active = true;
        loader2.active = true;
    }
}

Got a way to access this:有办法访问这个:

 InputRow{
       id: userForm
       property var userInput1
       property var userInput2

       component : 
           Column{
...
          TextInput {
                   id:val1
                   text: "777"
                   onTextChanged: userForm.userInput1 = text
                   Component.onCompleted : userForm.userInput1 = text 
               }
           }
MenuButton{
   onClicked : console.log(userForm.userInput1)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM