[Qt] fontawesome图标

fontawesome图标

fontawesome是一个图标的集合,里面有好多的图标,使用起来也还是非常方便的。
图标信息可以到官网去查:http://fontawesome.io/cheatsheet/
fontawesome-webfont.ttf 下载地址:http://pan.baidu.com/s/1sjyvp3v

下面的这个类是网络上的,我也找不到到底是谁的了。😄

//iconhelper.h

#ifndef ICONHELPER_H
#define ICONHELPER_H

#include <QObject>
#include <QFont>
#include <QFontDatabase>
#include <QMutex>
#include <QLabel>
#include <QPushButton>
#include <QApplication>
#include <QTreeWidgetItem>

class IconHelper : public QObject
{
private:
    explicit IconHelper(QObject *parent = 0);
    QFont iconFont;
    static IconHelper* _instance;

public:
    static IconHelper* Instance()
    {
        static QMutex mutex;
        if(!_instance)
        {
            QMutexLocker locker(&mutex);
            if(!_instance)
            {
                _instance = new IconHelper;
            }
        }
        return _instance;
    }

    void SetIcon(QLabel* lab, QChar c, int size = 10);
    void SetIcon(QPushButton* btn, QChar c, int size = 10);
};

#endif // ICONHELPER_H</pre>

//iconhelper.cpp

#include "iconhelper.h"

IconHelper* IconHelper::_instance = 0;
IconHelper::IconHelper(QObject *) :
    QObject(qApp)
{
    int fontId = QFontDatabase::addApplicationFont(":/image/fontawesome-webfont.ttf");
    QString fontName = QFontDatabase::applicationFontFamilies(fontId).at(0);
    iconFont = QFont(fontName);
}

void IconHelper::SetIcon(QLabel *lab, QChar c, int size)
{
    iconFont.setPointSize(size);
    lab->setFont(iconFont);
    lab->setText(c);
}

void IconHelper::SetIcon(QPushButton *btn, QChar c, int size)
{
    iconFont.setPointSize(size);
    btn->setFont(iconFont);
    btn->setText(c);
}</pre>

//调用方式

QPushButton * Btn = new QPushButton(widget);
IconHelper::Instance()->SetIcon(Btn, QChar(0xf192), 12);</pre>
posted @ 2015-09-26 20:50  MDGSF  阅读(2840)  评论(0编辑  收藏  举报