Why is it impossible to call console.log from the js function in QML[SOLVED]



  • I want to call the console.log() from the js function. Here's the example:

    function widthChanged() {
            console.log("Width = ", imageId.implicitWidth);
        }
    
    Image {
        onWidthChanged: widthChanged()
    }
    

    The output is: qrc:/qml/home.qml:35:. Just empty line.
    But when I call console.log() directly all stuff works. Example:

    Image {
        onWidthChanged: console.log(imageId.width)
    }
    

    P.S.
    I'm working not in the main.qml, the structure of my qml file looks like this:

    Rectangle {
        Image {
        }
    }
    


  • I'd try naming your function differently. There are naming conflicts with using widthChanged() since Image already has a widthChanged(). Try imageWidthChanged() as a name or something.

    If you really want to use widthChanged(), you could give an id to the parent of the widthChanged() js function. See below:

    Window {
        id: win
        visible: true
        width: 300
        height: 300
    
        Rectangle {
            id: rect
            color: "red"
            width: 100
            height: 100
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    rect.width += 10;
                }
            }
            onWidthChanged: win.widthChanged()
        }
        function widthChanged() {
            console.log("rectwidthchanged")
        }
    }


  • @dcape yeah, you're absolutely right. thanks for your efforts. I was thinking about conflicts and checked widthChanged() with auto-completion - this is why i decided not to change the title of js function. Thanks, again ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.