【Qt Quick】Image

加载图片

Image {
      width: 130; height: 100
      source: "../Image/TBH.png"
  }

不透明度

opacity: 0.5

拉伸图片的方法

说明

在不同场景中图片的显示方法也不同,平铺拉伸等。

写法

1. Stretch(默认)

图像被缩放以适应

Image {
      width: 130; height: 100
      source: "qtlogo.png"
  }

2. PreserveAspectFit

图像等比缩放

Image {
      width: 130; height: 100
      fillMode: Image.PreserveAspectFit
      source: "qtlogo.png"
  }

3. PreserveAspectCrop

图像均匀缩放以填充,必要时裁剪

Image {
      width: 130; height: 100
      fillMode: Image.PreserveAspectCrop
      source: "qtlogo.png"
      clip: true
  }

4. Tile

平铺

 Image {
      width: 120; height: 120
      fillMode: Image.Tile
      horizontalAlignment: Image.AlignLeft
      verticalAlignment: Image.AlignTop
      source: "qtlogo.png"
  }

5. TileVertically

垂直拉伸

Image {
      width: 120; height: 120
      fillMode: Image.TileVertically
      verticalAlignment: Image.AlignTop
      source: "qtlogo.png"
  }

6. TileHorizontally

水平拉伸


 Image {
      width: 120; height: 120
      fillMode: Image.TileHorizontally
      verticalAlignment: Image.AlignLeft
      source: "qtlogo.png"
  }

抗锯齿处理

较大图片缩小显示时,会有明显的锯齿感
在这里插入图片描述
处理之后的图片会明显平滑很多
在这里插入图片描述
手动进行设置大小

sourceSize: Qt.size(60,60)

绝对路径的图片加载方法

在绝对路径前+file:///

代码

说明

在面板中输入网址点击Open进行加载,可以加载网络图片或者本地图片。

import QtQuick 2.0
import QtQuick.Controls 2.5

Item {
    id:root
    anchors.fill: parent

    Rectangle{
        anchors.top:parent.top
        width: parent.width
        height: parent.height - 80
        color: "#121212"

        //设置等待加载时的转圈
        BusyIndicator{
            id:busy
            running: true
            anchors.centerIn: parent
            z:2
        }

        Text{
            id:stateLabel
            visible: false
            color: "white"
            font.pointSize: 30
            anchors.centerIn: parent
            z:3
        }

        Image {
            id: imageViewer
            asynchronous: true//网络加载时此属性不起作用
            cache:false//为false告诉图片不用缓存
            anchors.fill: parent
            fillMode: Image.PreserveAspectCrop //图片被均匀的缩放用来适应不需要裁剪的情况
            onStatusChanged: {
                if(imageViewer.status === Image.Loading){
                    busy.running = true
                    stateLabel.visible = false
                }else if(imageViewer.status === Image.Ready){
                    busy.running = false
                }else if(imageViewer.status === Image.Error){
                    busy.running = false
                    stateLabel.visible = true
                    stateLabel.text = "ERROR"
                }
            }
        }

//        Component.onCompleted: {
//            imageViewer.source =
//            "file:///D:\\aFX_WP\\Qt_code\\LoderImage\\maps\\3318.jpg"
//        }
    }

    Button{
        id:i_BtnOpen
        width: 80
        height: 30
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 20
        anchors.left: parent.left
        anchors.leftMargin: 20
        Text{
            text:"Open"
            anchors.centerIn: parent
            font.pointSize: 18
        }
        onClicked: {
            imageViewer.source = i_TInput_Path.text
        }
    }
    Rectangle{
        width: 400
        height: i_BtnOpen.height
        anchors.left: i_BtnOpen.right
        anchors.leftMargin: 20
        anchors.top: i_BtnOpen.top
        color: "black"
        TextInput{
            id:i_TInput_Path
            anchors.fill: parent
            color: "light gray"
            font.pointSize: 14
            verticalAlignment: Text.AlignVCenter
            selectByMouse: true//允许鼠标选择输入框内的文字
            selectionColor: "sky blue"
            clip: true
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非西昂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值