QML 封装使用SVG 和Iconfont

本文介绍了如何在QtQuick中使用SVG图标和Iconfont。SVG提供了一种异步加载、性能优化的图标配适方式,而Iconfont则通过Text组件和FontAwesome字体实现图标展示。代码示例展示了两种方法的实现细节,适用于UI界面的图标显示需求。
摘要由CSDN通过智能技术生成

直接上代码

1、使用SVG图
import QtQuick 2.6

/* https://www.iconfont.cn
    svg 是一种比较流行的icon 源。
    默认是异步加载的,可提高性能。
    因为需要寻找svg资源,这点没有AwesomeIcon 方便。
*/
/*! TODO */
Item {
    id:toou2d_svgicon
    width:  16; height: 16;

    property bool   asynchronous : true;

    property bool   smooth: true;

    property color  color;

    property string source;

    property alias status: image.status;

    Image {
        id:image
        asynchronous: toou2d_svgicon.asynchronous;
        anchors.fill: toou2d_svgicon;
        source:  toou2d_svgicon.source;
        smooth: toou2d_svgicon.smooth
        visible: false;
        enabled: false;
    }

    ShaderEffect {
        id: shaderItem
        property variant source: image
        property color   color: toou2d_svgicon.color
        enabled: false;
        fragmentShader: "qrc:/font/svg.cso"
        anchors.fill: parent;
        visible: toou2d_svgicon.status === Image.Ready;
    }
}

2、使用Iconfont

import QtQuick 2.6

/*!
    \qmltype TAwesomeIcon
    \inqmlmodule QtQuick 2.6
    \brief Font Icon
    \since 5.9.x
    \ingroup qtquicktest

    has been supported to the latest version

    \code
    TAwesomeIcon{
        source:T2D.FA_apple
    }
    \endcode

    Theme ini file is configured to 'FA_apple'.
*/
Item {
    id:toou2d_awesomeicon
    width:  16;
    height: 16;

    property color  color;

    /*! \sa TAwesomeType */
    property alias source : content_text.text;

    Text {
        id:content_text
        anchors.fill: parent;
        enabled:          false;
        color:            toou2d_awesomeicon.color;
        font.family:      "fontawesome"
        font.pixelSize:   Math.max(toou2d_awesomeicon.width,toou2d_awesomeicon.height);
        horizontalAlignment: Text.AlignHCenter;
        verticalAlignment:   Text.AlignVCenter; 
    }

}

具体代码参考

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值