• Qt加载SVG图片以及改变SVG图片颜色


    1 Qt加载SVG图片

    QTreeWidgetItem *item = new QTreeWidgetItem;
    //svg_path为SVG图片路径
    QSvgRenderer *svg_render = new QSvgRenderer(svg_path);
    QPixmap *pixmap = new QPixmap(32, 32);
    pixmap->fill(Qt::transparent);
    QPainter painter(pixmap);
    svg_render->render(&painter);
    QIcon ico(*pixmap);
    item->setIcon(1, ico);

    2 Qt改变SVG图片颜色

    QTreeWidgetItem *item = new QTreeWidgetItem;
    QString path = svg_path;
    QFile file(path);
    file.open(QIODevice::ReadOnly);
    QByteArray data = file.readAll();
    QDomDocument doc;
    doc.setContent(data);
    file.close();
    
    QString color = "blue";
    
    SetSVGBackColor(doc.documentElement(), "path", "fill", color);
    
    QByteArray svg_content = doc.toByteArray();
    QSvgRenderer *svg_render = new QSvgRenderer(svg_content);
    QPixmap *pixmap = new QPixmap(32, 32);
    pixmap->fill(Qt::transparent);
    QPainter painter(pixmap);
    svg_render->render(&painter);
    QIcon ico(*pixmap);
    item->setIcon(0, ico);
    void SetSVGBackColor(QDomElement &elem, QString strtagname, QString strattr, QString strattrval);
    
    void SetSVGBackColor(QDomElement &elem, QString strtagname, QString strattr, QString strattrval)
    {
        if (elem.tagName().compare(strtagname) == 0)
        {
            QString before_color = elem.attribute(strattr);
            elem.setAttribute(strattr, strattrval);
            QString color = elem.attribute(strattr);
        }
        for (int i = 0; i < elem.childNodes().count(); i++)
        {
            if (!elem.childNodes().at(i).isElement())
            {
                continue;
            }
            SetSVGBackColor(elem.childNodes().at(i).toElement(), strtagname, strattr, strattrval);
        }
    }
  • 相关阅读:
    sql语句游标的写法
    oracle的安装与plsql的环境配置
    oracle中创建表时添加注释
    jsp中Java代码中怎么获取jsp页面元素
    sql模糊查询
    jQuery循环给某个ID赋值
    Codeforces Round #671 (Div. 2)
    TYVJ1935 导弹防御塔
    Educational Codeforces Round 95 (Rated for Div. 2)
    Codeforces Round #670 (Div. 2)
  • 原文地址:https://www.cnblogs.com/LuckCoder/p/11171666.html
Copyright © 2020-2023  润新知