June 15, 2011

David Talmage David Talmage
Lab Rat
73 posts

Strange offset after rotating a rectangle

 

What happens to the space surrounding a Rectangle after it’s rotated? I have observed that if I rotate +/- 90 degrees a Rectangle with these anchors:

  1.  anchors.left: parent.left
  2.  anchors.verticalCenter: parent.verticalCenter

or these anchors

  1.  anchors.right: parent.right
  2.  anchors.verticalCenter: parent.verticalCenter

the Rectangle is offset from the edge of its parent. However, if I create a Rectangle of the same size but with the width and height swapped, the Rectangle is drawn at the edge of its parent as I intended.

The code and screenshot below illustrates the situation. The 150 X 40 yellow “Configure” Rectangle is not rotated. It is drawn where it should be: at the right edge of the Blue rectangle and centered on the blue Rectangle’s vertical center. The 40 X 150 green “Help” Rectangle is rotated. While it is centred on the blue Rectangle’s vertical center, it is many pixels to the right of the left edge of the blue Rectangle.

Screenshot of the Rectangle example

  1. import QtQuick 1.0
  2.  
  3. Rectangle {
  4.     id: container
  5.     height: 480
  6.     width: 800
  7.     color: "blue"
  8.  
  9.     Rectangle {
  10.  id: rightButton
  11.  anchors.right: parent.right
  12.  anchors.verticalCenter: parent.verticalCenter
  13.  width: 40
  14.  height: 150
  15.  color: "yellow"
  16.  
  17.  Text {
  18.      id: rightButtonText
  19.      text: "Configure"
  20.      rotation: 90
  21.      anchors.centerIn: parent
  22.  }
  23.     }
  24.  
  25.     Rectangle {
  26.  id: leftButton
  27.  rotation: -90
  28.  anchors.left: parent.left
  29.  anchors.verticalCenter: parent.verticalCenter
  30.  width: 150
  31.  height: 40
  32.  color: "green"
  33.  
  34.  Text {
  35.      id: leftButtonText
  36.      text: "Help"
  37.      anchors.centerIn: parent
  38.  }
  39.     }
  40.  
  41.     Text {
  42.  text: "Click to Exit"
  43.  anchors.centerIn: parent
  44.     }
  45.  
  46.     MouseArea {
  47.  anchors.fill: parent
  48.  onClicked: {
  49.      Qt.quit();
  50.  }
  51.     }
  52. }

3 replies

June 15, 2011

zester zester
Lab Rat
88 posts

Try horizontally centering also. And then adjust your child rectangle size.

June 16, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

Such behavior is ok. It’s all because of transformOrigin property is Center by default, so your rotation is done from center of your rectangle. In your case you need TopLeft value and don’t forget to set correct verticalCenterOffset (because your rectangle will be a bit higher than needed)

June 16, 2011

David Talmage David Talmage
Lab Rat
73 posts

I get it. Thanks. These kinds of transformations have always confused me.

 
  ‹‹ qml, how to parse text to number      Loading/Unloading images ››

You must log in to post a reply. Not a member yet? Register here!