2014-09-25 146 views
0

我在QML(QtQuick 1.0)中有一个图像对象,它的高度必须是相对的(在窗口缩放的情况下),但问题是当我尝试以全尺寸显示这个图像时。请看看我的代码如下部分:QML改变图像大小

Image { 
     id: selectionDialogImage 
     source: "qrc:/Images/myImage.png" 
     property int oldHeight: sourceSize.height 
     sourceSize.height: testsList.height/3 
     scale: 1 
     anchors.bottom: parent.bottom 
     anchors.left: parent.left 
     visible: false 
     property bool imageFullsize: false 
     MouseArea { 
       anchors.fill: parent 
       onClicked: 
        { 
        if(parent.imageFullsize) 
        { 
         parent.parent = selectionDialogQuestionText; 
         parent.sourceSize.width = undefined; 
         parent.sourceSize.height = testsList.height/3; 
         parent.anchors.horizontalCenter = undefined; 
         parent.anchors.verticalCenter = undefined; 
         parent.anchors.left = selectionDialogQuestionText.left; 
         parent.anchors.bottom = selectionDialogQuestionText.bottom; 
         parent.imageFullsize = false; 
        } 
        else 
        { 
         parent.parent = mainItem; 
         parent.sourceSize.height = parent.oldHeight; 
         //parent.sourceSize.height = undefined; 
         parent.sourceSize.width = mainItem.width; 
         parent.anchors.horizontalCenter = mainItem.horizontalCenter; 
         parent.imageFullsize = true; 
        } 
        } 
       } 
     } 

selectionDialogQuestionText是我的形象项的默认父。 mainItem是最大的项目,它具有整个窗口的大小。所以我希望在全屏显示时根据宽度设置图像大小,但在另一种状态下,我想缩放设置其高度的图像。

当我设置parent.sourceSize.width = mainItem.width;时,图像没有缩放,但其高度与之前(非常小)不同,因此其比例不合适。

我可以以任何方式存储旧高度的源图像吗?我需要类似const的东西,因为property int oldHeight: sourceSize.heigh是相对的。或者有没有办法恢复图像的默认大小,然后设置高度/宽度?

更新#1: 我试图用由@米奇描述的方法:

property int oldHeight 
Component.onCompleted: { 
    oldHeight = sourceSize.height 
    } 
    sourceSize.height: 250 

console.log("oldHeight="+parent.oldHeight)下面几行示出了oldHeight=250,不是原来的高度。

+0

你可以提供你想要做什么说明?你的问题很难遵循。 – Mitch 2014-09-25 08:11:50

回答

1

我可以以任何方式存储旧图像高度的源图像吗?

您可以使用Component.onCompleted

Image { 
    // ... 
    Component.onCompleted: { 
     oldHeight = sourceSize.height 
    } 
} 
+0

它不起作用。在第一篇文章中查看我的更新。 – trivelt 2014-09-25 08:32:36

+1

如果你设置'sourceSize.height:250',那么它总是250,因为在'Component.onCompleted'运行之前评估它。您应该在'Component.onCompleted'结尾处设置'sourceSize.height = 250'。 – Mitch 2014-09-25 12:27:51

+0

是的,谢谢。我前段时间了解它,但它并没有解决我的问题,原因是'sourceSize.height'的原始值是...奇怪。最后,我没有调整图像大小,而是创建了另一个Rectangle(包含Image {})作为mainItem(根元素)的子元素。所以''onClicked'我只有'imageFullscreenBox.imgSource = parent.source;'和'imageFullscreenBox.visible = true;' – trivelt 2014-09-25 13:05:35