首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在QML - sourceClipRect中只显示图像的右边部分

在QML - sourceClipRect中只显示图像的右边部分
EN

Stack Overflow用户
提问于 2022-05-04 14:34:32
回答 1查看 90关注 0票数 0

我试图使用sourceClipRect属性在QML中只显示图像的正确部分

下面是代码的一个片段

代码语言:javascript
运行
复制
Image
{
    id : _image

    anchors.horizontalCenter: parent.horizontalCenter
    anchors.verticalCenter: parent.verticalCenter 
    fillMode: Image.PreserveAspectCrop
    width: parent.width
    height: parent.height
    sourceSize.width : undefined
    sourceSize.height : undefined
    source : imagePath  
    sourceClipRect: Qt.rect(sourceSize.width/2, 0, sourceSize.width/2, sourceSize.height)
    smooth : true
    mipmap : true
}

此图像位于具有固定宽度和高度的项内。

这给了我一个警告和一个属性sourceClipRect的绑定循环,我猜是因为在sourceClipRect中使用了sourceSize

我不能在sourceClipRect中使用硬数字,因为我不知道图片的原始大小

你知道怎样才能避免这个绑定循环吗?也许通过另一种方法获得原始的宽度和高度,但我不知道在纯QML中除了sourceSize以外的任何其他方法

Ps :结果和预期的一样工作,并且运行良好,我只是有一个丑陋的警告声明绑定循环。

提前谢谢

EN

Stack Overflow用户

回答已采纳

发布于 2022-05-10 09:53:44

我终于找到了一个可以接受的解决办法。这可能不是最干净的,但我可以接受。

但是在图像的实例化中,我只是保存var而没有绑定,如下所示

代码语言:javascript
运行
复制
  Image
  {
      id : _image

    property var  sourceSizeWidth :{sourceSizeWidth = sourceSize.width} //initializing this way avoids the binding between sourceSize.Width and sourceSizeWidth
    property var  sourceSizeHeight :{sourceSizeHeight = sourceSize.height} //initializing this way avoids the binding between sourceSize.height and sourceSizeHeight

    anchors.horizontalCenter: parent.horizontalCenter
    anchors.verticalCenter: parent.verticalCenter 
    fillMode: Image.PreserveAspectCrop
    width: parent.width
    height: parent.height
    sourceSize.width : undefined
    sourceSize.height : undefined
    source : imagePath  
    sourceClipRect: Qt.rect(sourceSizeWidth/2, 0, sourceSizeWidth/2, sourceSizeHeight) //Use the unbinded var to set the sourceClipRect
    smooth : true
    mipmap : true
   }

这样的初始化避免了新创建的sourceSizeWidth和QML属性sourceSize.width之间的绑定。

代码语言:javascript
运行
复制
 property var  sourceSizeWidth :{sourceSizeWidth = sourceSize.width}

正如我所说的,这并不是最干净的,而且可能有一种更聪明的方法,但是现在它已经足够了,它工作得很好,而且没有警告绑定循环。

干杯!

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72114804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档