Results 1 to 2 of 2

Thread: How to achieve velocity of a moving component

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2016
    Posts
    50
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default How to achieve velocity of a moving component

    Below is a simplified version of a bigger QML program where a waiting loop (while) in a function is used to measure the time of an event (collision).
    Everything in the code looks fine to me, and we expect when the collision occurs, the while loop terminates by the function's return statement, but in reality it crashes, I don't know why!

    Qt Code:
    1. import QtQuick 2.12
    2. import QtQuick.Window 2.12
    3.  
    4. Window {
    5. id: window
    6. visible: true
    7. width: 1000; height: 800
    8. color: "royalblue"
    9. property real colTime_1: new Date().getTime()
    10. property real colTime_2
    11. property real speed
    12.  
    13. Rectangle {
    14. id: ball
    15. x: window.x + 50; y: window.y + 50
    16. width: 18; height: 18
    17. color: "white"
    18. radius: width/2
    19. }
    20.  
    21. // Move the ball
    22. onFrameSwapped: {
    23. ball.x += 5
    24. ball.y += 3
    25. }
    26.  
    27. Component.onCompleted: {
    28. if (catchColTime()) console.log("Speed = ", speed)
    29. }
    30.  
    31. function catchColTime() {
    32. while(true)
    33. if (ball.x >= window.width) {
    34. colTime_2 = new Date().getTime()
    35. speed = window.width / (colTime_2 - colTime_1)
    36. return true
    37. }
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2008
    Posts
    383
    Thanks
    9
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to achieve velocity of a moving component

    Hi, you should check with a debugger when and where the crash happens.
    For example this line
    speed = window.width / (colTime_2 - colTime_1)
    might cause a crash if colTime_2 and colTime_1 are the same (division by zero).

    Ginsengelf

Similar Threads

  1. How to achieve Alt+Tab effect ?
    By y.s.bisht in forum Newbie
    Replies: 0
    Last Post: 11th November 2011, 14:22
  2. Replies: 0
    Last Post: 24th May 2010, 13:19
  3. Replies: 1
    Last Post: 30th November 2007, 11:03
  4. How to achieve showAll ?
    By Gopala Krishna in forum Qt Programming
    Replies: 5
    Last Post: 5th October 2007, 15:26
  5. How to achieve Uniformity...!!!
    By deepusrp in forum Qt Programming
    Replies: 9
    Last Post: 7th May 2007, 10:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.