Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to access property alias



  • There is a variable declared in this QML file and I was wondering if I will have access to it from another QML file? The variable is declared as property alias:

    property alias displayValue: currentValue.text
    

    is it possible to access this variable from any other QML file? I tried saying:

     property string inputValue: InputField.currentValue.text
    

    InputField turns green when I call it like this so I think it's recognizing the correct QML file but it's not working:

    TypeError: Cannot read property 'text' of undefined
    

  • Qt Champions 2017

    Access it like follows.

    InputField.displayValue



  • @dheerendra For some reason it is telling me the value is undefined:

    Unable to assign [undefined] to QString
    

  • Qt Champions 2017

    What is InputField ?



  • @dheerendra InputField is the name of the QML file which creates the instance of this keyboard. It is also the id of this AbstractInputField within the script:

    id: inputField
        property alias displayValue: currentValue.text
        property alias placeholderText: currentValue.placeholderText
        property alias validator: editingValue.validator
        property int horizontalAlignment: TextInput.AlignLeft
        property bool doSelection: true
        readonly property alias isEditing: editingValue.visible
    
        signal accepted(string value)
        height: style.height
    
        implicitWidth: editingValue.implicitWidth
        implicitHeight: editingValue.implicitHeight
    
        QQC2.TextField {
            id: currentValue
            anchors.fill: parent
            background: Rectangle{
                anchors.fill: parent
                radius: inputField.style.radius
                border.width: inputField.style.fieldOutlineSize
                border.color: inputField.style.fieldOutlineColor
            }
    
            readOnly: true
            horizontalAlignment: inputField.horizontalAlignment
            visible: !(editingValue.activeFocus)
            font: inputField.style.font.font
            color: inputField.readOnly ? inputField.style.readOnlyFontColor : inputField.style.inputFontColor
    
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    if (!inputField.readOnly)
                        editingValue.forceActiveFocus()
                }
            }
        }
    
        QQC2.TextField {
            id: editingValue
            focus: true
            height: inputField.style.height
            visible: activeFocus
            horizontalAlignment: inputField.horizontalAlignment
            onAccepted: {
                inputField.accepted(text)
                keyboardPanel.target = null
                currentValue.forceActiveFocus()
            }
    
            onActiveFocusChanged: {
                if (activeFocus) {
                    text = currentValue.text
                    if (doSelection)
                        selectAll()
                    keyboardPanel.target = ( presentationManager.guiSettingsPresenter.model.virtualKeyboardEnabled
                                             ? inputField
                                             : null )
                } else {
                    inputField.accepted(text)
                    keyboardPanel.target = null
                }
            }
        }
    
        function cancel() {
            keyboardPanel.target = null
            currentValue.forceActiveFocus()
        }
    }
    
    

    I really just need access to the displayValue outside of InputField. Perhaps I could do that with signals?


Log in to reply