Simple Transformations

A transformation manipulates the geometry of an object. QML Items can, in general, be translated, rotated and scaled. There is a simple form of these operations and a more advanced way.

Let’s start with the simple transformations. Here is our scene as our starting point.

A simple translation is done via changing the x,y position. A rotation is done using the rotation property. The value is provided in degrees (0 .. 360). A scaling is done using the scale property and a value <1 means the element is scaled down and >1 means the element is scaled up. Rotation and scaling do not change an item’s geometry: the x,y and width/height haven’t changed; only the painting instructions are transformed.

Before we show off the example I would like to introduce a little helper: the ClickableImage element. The ClickableImage is just an image with a mouse area. This brings up a useful rule of thumb - if you have copied a chunk of code three times, extract it into a component.

  1. // ClickableImage.qml
  2. // Simple image which can be clicked
  3. import QtQuick
  4. Image {
  5. id: root
  6. signal clicked
  7. MouseArea {
  8. anchors.fill: parent
  9. onClicked: root.clicked()
  10. }
  11. }

Simple Transformations - 图1

We use our clickable image to present three objects (box, circle, triangle). Each object performs a simple transformation when clicked. Clicking the background will reset the scene.

  1. // transformation.qml
  2. import QtQuick
  3. Item {
  4. // set width based on given background
  5. width: bg.width
  6. height: bg.height
  7. Image { // nice background image
  8. id: bg
  9. source: "assets/background.png"
  10. }
  11. MouseArea {
  12. id: backgroundClicker
  13. // needs to be before the images as order matters
  14. // otherwise this mousearea would be before the other elements
  15. // and consume the mouse events
  16. anchors.fill: parent
  17. onClicked: {
  18. // reset our little scene
  19. circle.x = 84
  20. box.rotation = 0
  21. triangle.rotation = 0
  22. triangle.scale = 1.0
  23. }
  24. }
  25. ClickableImage {
  26. id: circle
  27. x: 84; y: 68
  28. source: "assets/circle_blue.png"
  29. antialiasing: true
  30. onClicked: {
  31. // increase the x-position on click
  32. x += 20
  33. }
  34. }
  35. ClickableImage {
  36. id: box
  37. x: 164; y: 68
  38. source: "assets/box_green.png"
  39. antialiasing: true
  40. onClicked: {
  41. // increase the rotation on click
  42. rotation += 15
  43. }
  44. }
  45. ClickableImage {
  46. id: triangle
  47. x: 248; y: 68
  48. source: "assets/triangle_red.png"
  49. antialiasing: true
  50. onClicked: {
  51. // several transformations
  52. rotation += 15
  53. scale += 0.05
  54. }
  55. }
  56. // ...

Simple Transformations - 图2

The circle increments the x-position on each click and the box will rotate on each click. The triangle will rotate and scale the image up on each click, to demonstrate a combined transformation. For the scaling and rotation operation we set antialiasing: true to enable anti-aliasing, which is switched off (same as the clipping property clip) for performance reasons. In your own work, when you see some rasterized edges in your graphics, then you should probably switch smoothing on.

TIP

To achieve better visual quality when scaling images, it is recommended to scale down instead of up. Scaling an image up with a larger scaling factor will result in scaling artifacts (blurred image). When scaling an image you should consider using smooth: true to enable the usage of a higher quality filter at the cost of performance.

The background MouseArea covers the whole background and resets the object values.

TIP

Elements which appear earlier in the code have a lower stacking order (called z-order). If you click long enough on circle you will see it moves below box. The z-order can also be manipulated by the z property of an Item.

Simple Transformations - 图3

This is because box appears later in the code. The same applies also to mouse areas. A mouse area later in the code will overlap (and thus grab the mouse events) of a mouse area earlier in the code.

Please remember: the order of elements in the document matters.