Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Why is it impossible to call console.log from the js function in QML[SOLVED]

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

Scheduled Pinned Locked Moved QML and Qt Quick
qmljavascriptconsolelog
3 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sosun
    wrote on 8 Oct 2015, 15:17 last edited by sosun 10 Sept 2015, 13:38
    #1

    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 {
        }
    }
    
    1 Reply Last reply
    0
    • D Offline
      D Offline
      dcape
      wrote on 8 Oct 2015, 18:28 last edited by dcape 10 Aug 2015, 18:44
      #2

      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")
          }
      }
      
      S 1 Reply Last reply 9 Oct 2015, 13:38
      1
      • D dcape
        8 Oct 2015, 18:28

        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")
            }
        }
        
        S Offline
        S Offline
        sosun
        wrote on 9 Oct 2015, 13:38 last edited by
        #3

        @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 ;)

        1 Reply Last reply
        0

        1/3

        8 Oct 2015, 15:17

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved