Qt 使用阿里图标库

Qt 使用阿里图标库

  • 解压:

  • 建立QT测试项目:
  • 目录结构:

  • ui界面:

  • 主要代码:
#include "widget.h"
#include "ui_widget.h"

#include <QFontDatabase>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setStyleSheet("background:#FAFAFA");

    int fontId = QFontDatabase::addApplicationFont(":/font/iconfont.ttf");  //加入字体,并获取字体ID
    QString fontName = QFontDatabase::applicationFontFamilies(fontId).at(0);    //获取字体名称
    QFont iconFont = QFont(fontName);
    iconFont.setPixelSize(36);

    ui->pLabel->setFont(iconFont);
    ui->pLabel->setText(QChar(0xe651));
    ui->pLabel->setStyleSheet("color:#a90afe;background:#eee");
    ui->pLabel->resize(54,54);

    ui->pLabel_2->setFont(iconFont);
    ui->pLabel_2->setText(QChar(0xeb2d));
    ui->pLabel_2->setStyleSheet("color:skyblue;background:#eee");
    ui->pLabel_2->resize(54,54);


    ui->pLabel_3->setFont(iconFont);
    ui->pLabel_3->setText(QChar(0xeb2c));
    ui->pLabel_3->setStyleSheet("color:#fc5531;background:#eee");
    ui->pLabel_3->resize(54,54);


    iconFont.setPixelSize(30);
    ui->pushButton->setFont(iconFont);
    ui->pushButton->setText(QChar(0xe61d));
    ui->pushButton->setStyleSheet("QPushButton{color:purple;border:0 solid #FFF;background:#eee}"\
                                  "QPushButton:hover{color:red}"\
                                  );
    ui->pushButton->resize(54,54);

}
  • 注:其中setText(QChar(0xeb2c));  中的 0xeb2c可以通过下载解压打开的demo_index.html进行查看

  • 运行结果:

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Qt使用阿里巴巴普惠体字体,你可以按照以下步骤进行操作: 1. 首先,获取阿里巴巴普惠体的字体文件。你可以通过下载阿里巴巴普惠体的压缩包并解压,得到字体文件。 2. 将获取到的阿里巴巴普惠体字体复制到你的Qt工程目录下的一个文件夹中,例如新建一个名为"font"的文件夹。 3. 在Qt的资源文件(.qrc)中添加这个字体文件。你可以在资源文件中新建一个前缀为"font"的资源,并将"font"文件夹下的阿里巴巴普惠体字体文件添加到资源中。 4. 在需要使用阿里巴巴普惠体的地方,你可以通过获取字体文件的路径来设置字体。你可以使用以下代码来获取字体文件的路径: ``` QString Widget::getFontNameFromFontfile(const QString &filePath) { QString font = ""; QFile fontFile(filePath); if(!fontFile.open(QIODevice::ReadOnly)) { qDebug() << "Open font file error"; return font; } int loadedFontID = QFontDatabase::addApplicationFontFromData(fontFile.readAll()); QStringList loadedFontFamilies = QFontDatabase::applicationFontFamilies(loadedFontID); if(!loadedFontFamilies.empty()) { font = loadedFontFamilies.at(0); } fontFile.close(); return font; } ``` 这段代码将会返回字体文件的路径。 5. 使用获取到的字体名称来设置需要使用阿里巴巴普惠体的控件的字体。你可以使用`setFont()`函数将字体应用到需要的控件上。 需要注意的是,阿里巴巴普惠体的粗体(Bold)可能需要特殊处理。在使用fontloader加载普惠体时,你可能会发现与设计稿对不上,Bold号字体比Medium号字体明显细,而其他字体则正常。所以,你可以尝试将Bold属性直接设为true。 以上是在Qt使用阿里巴巴普惠体字体的步骤和注意事项。希望对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Qt学习笔记:使用阿里巴巴的普惠体](https://blog.csdn.net/chase_hung/article/details/89814921)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Qt 阿里巴巴普惠体2.0的坑](https://blog.csdn.net/miku531/article/details/128017991)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值