首页 文章

QML改变图像的大小

提问于
浏览
0

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

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 ,而不是原始高度 .

1 回答

  • 1

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

    你可以使用 Component.onCompleted

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

相关问题