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

QML Problems



  • I have a QML file that contains the excerpt
    @
    Utils{
    id: myUtils
    }
    TextArea {
    id: myTextArea
    readOnly: true
    text: ""
    }
    TextField{
    id: myTextField
    placeholderText: "Enter text"
    }
    Button {
    id: myButton
    text: "Submit"
    onClicked: {
    myUtils.addRow("myTextArea", myTextField.text)
    }
    }
    @

    and a QML file Utils.qml that contains the excerpt
    @
    function addRow(textAreaId, text){
    textAreaId.append("<i>" + text + "</i>");
    }
    @

    The problems I'm having are:

    The error message "TypeError: Property 'append' of object myTextArea is not a function" is produced.

    It works when I replace textAreaId.append by myTextArea.append in addRow but I want a more general solution. [BTW, I am not interested in solutions that use eval.]

    "<i>" + text + "</i>" appears in myTextArea instead of text being italicized when I make the change indicated in 1.

    Any suggestions?

    Thanks.

    Steve



  • Because you are passing is as text, try to pass it by id

    inside the main.qml:

    Utils{
        id: mine
    }
    
    TextArea{
        id: mytf
        width: parent.width
        height: parent.height
    }
    
    MouseArea{
        anchors.fill: parent
        onClicked: {
            mine.ap(mytf,"mytest")
            console.debug("clicked")
        }
    }
    

    in Utils.qml:

    import QtQuick 2.0
    Rectangle {
    width: 100
    height: 62
    function ap(tf,t){
    tf.append(t)
    }
    }



  • Changing
    @
    myUtils.addRow("myTextArea", myTextField.text)
    @

    to
    @
    myUtils.addRow(myTextArea, myTextField.text)
    @

    solved the first problem. Any suggestions for the second problem?

    Thanks.

    Steve


Log in to reply