1、显示单个svg文件
创建一个文档,添加
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<!--! Font Awesome Free 6.1.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M256 0C114.6 0 0 114.6 0 256s114.6 256 256 256s256-114.6 256-256S397.4 0 256 0zM256 464c-114.7 0-208-93.31-208-208S141.3 48 256 48s208 93.31 208 208S370.7 464 256 464zM255.1 176C255.1 176 255.1 176 255.1 176c21.06 0 40.92 8.312 55.83 23.38c9.375 9.344 24.53 9.5 33.97 .1562c9.406-9.344 9.469-24.53 .1562-33.97c-24-24.22-55.95-37.56-89.95-37.56c0 0 .0313 0 0 0c-33.97 0-65.95 13.34-89.95 37.56c-49.44 49.88-49.44 131 0 180.9c24 24.22 55.98 37.56 89.95 37.56c.0313 0 0 0 0 0c34 0 65.95-13.34 89.95-37.56c9.312-9.438 9.25-24.62-.1562-33.97c-9.438-9.312-24.59-9.219-33.97 .1562c-14.91 15.06-34.77 23.38-55.83 23.38c0 0 .0313 0 0 0c-21.09 0-40.95-8.312-55.89-23.38c-30.94-31.22-30.94-82.03 0-113.3C214.2 184.3 234 176 255.1 176z"/>
</svg>
然后保存为svg格式的文件。
然后在Qt的pro中添加:QT +=svg
头文件添加:#include <QSvgRenderer>
调用svg和显示:
QSvgRenderer render;
render.load (QString("svg文件的路径"));
QPixmap *pix = new QPixmap(800,600);//显示大小
pix->fill (Qt::transparent);
QPainter painter(pix);
painter.setRenderHints (QPainter::Antialiasing);
render.render (&painter);
ui->pushButton->setIcon(QIcon(*pix));
ui->pushButton->setFlat(true);
2、显示svg的合集的方式
网页中好像是Symbol 引用,Qt中我们使用https://www.iconfont.cn网站先建立图标项目,图标整理完以后,下载项目,找到css中对图标的编号,再在QT资源中导入该ttf字体库资源。
int fontId = QFontDatabase::addApplicationFont("D:/3/untitled/picture/iconfont.ttf");
QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);
QFont font;
font.setFamily(fontFamilies.at(0));
font.setPointSize(100);
ui->pushButton->setFont(font);
QString str = QString("%1").arg(QChar(0xe600));//0xe600对应相应的控件图标
ui->pushButton->setText(str);
其中如果要修改对应的控件颜色,可以设置控件颜色color: rgb(255, 0, 0);来修改