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. Some strange component behavior
Forum Updated to NodeBB v4.3 + New Features

Some strange component behavior

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 365 Views 2 Watching
  • 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.
  • T Offline
    T Offline
    tomy
    wrote on 12 Feb 2019, 23:13 last edited by tomy 2 Dec 2019, 23:14
    #1

    Hi,

    Please look at this simple code. Why while the user hasn't still moved the racket, its y property is changed!? (Console writes "Y changed")

    main.qml:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    
    Window {
        id: window
        visible: true
        width: 700; height: 500
        color: "gray"
    
        Rectangle {
            id: table
            width: window.width / 1.15; height: window.height / 1.15
            y: 10
            anchors.horizontalCenter: parent.horizontalCenter
            color: "royalblue"
        }
    
        Racket {
            id: racket
            x: table.width - 10
            y: table.height / 2
        }
    }
    

    Racket.qml:

    import QtQuick 2.12
    
    Rectangle {
        width: 15; height: 65
        property int oldY: y
        property bool yUwards
        property bool yDwards
    
        onYChanged: {
            console.log("Y changed\n")
            if (y > oldY) yDwards = true
            else if (y < oldY) yUwards = true
            oldY = y
        }
    
        MouseArea {
            anchors.fill: parent
            anchors.margins: -parent.height
            drag.target: parent
            drag.axis: Drag.YAxis
            drag.minimumY: table.y
            drag.maximumY: table.height - parent.height + 10
        }
    }
    
    1 Reply Last reply
    0
    • O Offline
      O Offline
      oria66
      wrote on 13 Feb 2019, 01:59 last edited by
      #2

      @tomy. If you look carefully, the y property of Racket component instantiation is set to table.height/2. That is the moment and the cause because you see that message on the console. At the creation of the component the y change triggering the signal onYChanged. Maybe you must change your logic if you want to obtain a different behavior.

      By the way, it's not healthy to use properties of external components in your component. For example, Why you use table object inside Rocket.qml? It works now, but when the application grow that will be a problem. Besides, your Rocket component will be more portable. Imagine you want to use Rocket.qml in other application, or in other part of your program.

      My recommendation is to use local properties inside Rocket, something like:

      Rocket.qml

      import QtQuick 2.12
      
      Rectangle {
          width: 15; height: 65
      
          property real minY: 0
          property real maxY: 0
      
          property int oldY: y
          property bool yUwards
          property bool yDwards
      
          onYChanged: {
              console.log("Y changed\n")
              if (y > oldY) yDwards = true
              else if (y < oldY) yUwards = true
              oldY = y
          }
      
          MouseArea {
              anchors.fill: parent
              anchors.margins: -parent.height
              drag.target: parent
              drag.axis: Drag.YAxis
              drag.minimumY: minY
              drag.maximumY: maxY - parent.height + 10
          }
      }
      

      main.qml

      ...
             Racket {
                 id: racket
                 x: table.width - 10
                 y: table.height / 2
      
                 minY: table.y
                 maxY: table.height
             }
      ...
      

      Good luck...

      The truth is out there

      1 Reply Last reply
      1
      • Y Offline
        Y Offline
        Yashpal
        wrote on 13 Feb 2019, 07:25 last edited by
        #3

        Hi! the Y value for your racket component has a data binding "table.height / 2"
        I believe these type of bindings are evaluated only after the respective component is created and initialized, that is, the y property would first have default value(0) then would be assigned to the value evaluated from
        table.height / 2

        If suppose, if you have assigned y: 200 directly this would put y=200 during component initialization.

        1 Reply Last reply
        1

        1/3

        12 Feb 2019, 23:13

        • 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