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 callconsole.log()
directly all stuff works. Example:Image { onWidthChanged: console.log(imageId.width) }
P.S.
I'm working not in themain.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") } }