2014-10-18 18 views
0

说,我有一个FlickableImage里面的某个地方。我需要相应地更新contentWidthcontentHeight。尽管可以手动进行适当的计算,但是我想知道是否有办法获得Image(以及其他Item)的边界矩形?如何知道旋转项目的边界矩形

EDIT(响应于Mitch's suggestion):

乍一看它看起来像真棒例子。我甚至开始认为我在尝试使用Item.childrenRect属性组时错过了一些东西......不幸的是,修改了提供的示例以便边界围绕儿童项目绘制,我再次确信childrenRect的属性不要重视物品的旋转。请看下图:

import QtQuick 2.3 
import QtQuick.Controls 1.2 

Item { 
    width: 400 
    height: 400 

    Component.onCompleted: addChildItem() 

    function addChildItem() { 
     var rect = Qt.createQmlObject("import QtQuick 2.3; Rectangle {}", container); 
     rect.x = Math.random() * 200; 
     rect.y = Math.random() * 200; 
     rect.width = 64 * slider.value; 
     rect.height = 64 * slider.value; 
     rect.color = "green"; 
     rect.opacity = 0.5; 
     rect.rotation = Math.random() * 360; 
    } 

    Row { 
     anchors.horizontalCenter: parent.horizontalCenter 
     anchors.bottom: parent.bottom 

     Slider { 
      id: slider 
      value: 0.5 

      Text { 
       text: "Child item size" 
       anchors.horizontalCenter: parent.horizontalCenter 
       anchors.bottom: parent.top 
      } 
     } 

     Button { 
      text: "Add child item" 
      onClicked: addChildItem() 
     } 
    } 

    Item { 
     id: container 
     x: 50 
     y: 50 
    } 

    Item { 
     x: container.x 
     y: container.y 
     Rectangle { 
      x: container.childrenRect.x 
      y: container.childrenRect.y 
      width: container.childrenRect.width 
      height: container.childrenRect.height 
      color: "transparent" 
      border.color: "black" 
     } 
    } 
} 

回答

0

childrenRect应该给你考虑到旋转边界矩形,但有一个错误:childrenRect doesn't take transformations into account.


原来的答复:

使用childrenRect

import QtQuick 2.3 
import QtQuick.Controls 1.2 

Item { 
    width: 400 
    height: 400 

    Component.onCompleted: addChildItem() 

    function addChildItem() { 
     var rect = Qt.createQmlObject("import QtQuick 2.3; Rectangle {}", container); 
     rect.x = Math.random() * 100; 
     rect.y = Math.random() * 100; 
     rect.width = 64 * slider.value; 
     rect.height = 64 * slider.value; 
     rect.color = "green"; 
     rect.opacity = 0.5; 
     rect.rotation = Math.random() * 360; 
    } 

    Row { 
     anchors.horizontalCenter: parent.horizontalCenter 
     anchors.bottom: parent.bottom 

     Slider { 
      id: slider 
      value: 0.5 

      Text { 
       text: "Child item size" 
       anchors.horizontalCenter: parent.horizontalCenter 
       anchors.bottom: parent.top 
      } 
     } 

     Button { 
      text: "Add child item" 
      onClicked: addChildItem() 
     } 
    } 

    Item { 
     id: container 
     anchors.centerIn: parent 
    } 

    Text { 
     text: "Total rotated bounds" 

     Rectangle { 
      id: boundsRect 
      width: container.childrenRect.width 
      height: container.childrenRect.height 
      anchors.top: parent.bottom 
      color: "transparent" 
      border.color: "black" 
     } 
    } 
} 

example screenshot

+0

不幸的是,这并不是我所需要的。请参阅我的初始帖子的编辑。 – 2014-10-18 19:53:55

+0

对......我现在依稀记得这个;有一个childrenRect的错误。我已经更新了我的答案。 – Mitch 2014-10-18 20:20:53

+0

投诉bug报告。将等待它被压扁。我事先接受你的回答,以便这个问答对可能在将来变得相关。 – 2014-10-18 20:55:32