【问题标题】:QML changing size of imageQML改变图像的大小
【发布时间】:2014-09-25 07:30:18
【问题描述】:

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

 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: 我尝试使用@Mitch 描述的方法:

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

console.log("oldHeight="+parent.oldHeight) 下面几行显示oldHeight=250,而不是原始高度。

【问题讨论】:

  • 你能提供你想要做什么的插图吗?你的问题很难理解。

标签: image qt qml


【解决方案1】:

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

你可以使用Component.onCompleted:

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

【讨论】:

  • 如果您设置sourceSize.height: 250,那么它将始终为 250,因为这是在运行 Component.onCompleted 之前评估的。您应该在Component.onCompleted 的末尾设置sourceSize.height = 250
  • 好的,谢谢。前段时间我明白了,但它并没有解决我的问题,因为 sourceSize.height 的原始值是......奇怪。最后,我没有调整图像大小,而是创建了另一个 Rectangle(包含 Image{})作为 mainItem(根元素)的子项。所以在onClicked 我只有imageFullscreenBox.imgSource = parent.source;imageFullscreenBox.visible = true;
猜你喜欢
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
  • 2013-12-07
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 2013-12-31
  • 1970-01-01
相关资源
最近更新 更多