Results 1 to 5 of 5

Thread: Rotating a line without moving it in QML

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Rotating a line without moving it in QML

    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Item
    4. {
    5. width: 660
    6. height: 660
    7.  
    8. Rectangle
    9. {
    10. id : dial
    11. width: 360
    12. height: 360
    13.  
    14. color: "gray"
    15.  
    16. Rectangle
    17. {
    18. id: dot
    19. height: 5
    20. width: 5
    21. color: "red"
    22. x: dial.x + (dial.width/2);
    23. y: dial.y + (dial.height/2);
    24. }
    25.  
    26. Image
    27. {
    28. id: line
    29. source: "/home/.../documents/test/straightLine.jpg"
    30. height: 50
    31. width: 50
    32. anchors.horizontalCenter: parent.horizontalCenter
    33. anchors.verticalCenter: parent.verticalCenter
    34. transform: Rotation
    35. {
    36. origin.x: dial.x + (dial.width/2);
    37. origin.y: dial.y + (dial.height/2);
    38. angle: 40
    39. }
    40. }
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 

    The dot is a representation of the origin point. The line's center point should stay at that origin.

    When I apply the angle : 40, the line moves away from its origin.

    How to tell it to say at that origin while rotating?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Rotating a line without moving it in QML

    Use anchors instead of specifying x and y of all elements directly. And use the "rotation" property instead of applying a Rotation element. The latter is useful if you want to transform around an arbitrary point instead of "transformationOrigin".

    You probably want:

    javascript Code:
    1. import QtQuick 2.0
    2.  
    3. Item
    4. {
    5. width: 660
    6. height: 660
    7.  
    8. Rectangle
    9. {
    10. id : dial
    11. width: 360
    12. height: 360
    13.  
    14. color: "gray"
    15.  
    16. Rectangle
    17. {
    18. id: dot
    19. height: 5
    20. width: 5
    21. color: "red"
    22. anchors.centerIn: parent
    23. }
    24.  
    25. Image
    26. {
    27. id: line
    28. source: "/home/.../documents/test/straightLine.jpg"
    29. height: 50
    30. width: 50
    31. anchors.centerIn: parent
    32. rotation: 40
    33. }
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    TheIndependentAquarius (5th June 2014)

  4. #3
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: Rotating a line without moving it in QML

    Quite thankful to you. That indeed works easily.

    I'd be more grateful though if you could explain what was wrong with my code too.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Rotating a line without moving it in QML

    "x" and "y" are defined relatively to the parent. Thus you cannot reference other item's "x" and "y" values as they are in a different coordinate system. This is similar to:

    javascript Code:
    1. Item {
    2. id: i1
    3. x: 10
    4. Item {
    5. id: i2
    6. x: 10
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Item "i2" has an absolute x value set to 20 as it is translated by 10 relative to its parent. If instead you do:
    javascript Code:
    1. Item {
    2. id: i1
    3. x: 10
    4. Item {
    5. id: i2
    6. x: parent.x+10
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Then i2.x == 20 and thus the absolute value is 30 and not 20.

    In your code the transformation origin should be set to:

    javascript Code:
    1. origin.x: line.width/2;
    2. origin.y: line.height/2;
    To copy to clipboard, switch view to plain text mode 

    which is equivalent to setting transformOrigin to Item.Center (which is the default).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    TheIndependentAquarius (5th June 2014)

  7. #5
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: Rotating a line without moving it in QML

    Thankful to you again.

Similar Threads

  1. rotating a qwtplot
    By pellegrini in forum Qwt
    Replies: 2
    Last Post: 20th March 2014, 09:11
  2. Rotating Items
    By PauloF91 in forum Newbie
    Replies: 7
    Last Post: 27th May 2013, 23:46
  3. Rotating Gradient
    By JeffC in forum Newbie
    Replies: 3
    Last Post: 3rd June 2012, 11:11
  4. Rotating pixmap
    By jano_alex_es in forum Newbie
    Replies: 4
    Last Post: 19th December 2010, 04:00
  5. Replies: 0
    Last Post: 24th May 2010, 12:19

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.