Help us understand the problem. What is going on with this article?

FontAwesome で動的にアイコン化 in Qt

More than 5 years have passed since last update.

自分含めて FontAwesome などのアイコンフォントを使ってアイコンを書き出したいという需要があると思うので。

// iconSize と fontPointSize は適当に設定している
const QChar iconType = 0xf000; // css/font-awesome.css に定義がある。ここでは .icon-glass と同じ
const int iconSize = 52;
const int fontPointSize = 42;

// フォントをインストールする
int fontID = QFontDatabase::addApplicationFont("/path/to/font-awesome.ttf");

// QPixmap を生成して透明にしておく
QPixmap pixmap(iconSize, iconSize);
pixmap.fill(Qt::transparent);

// QFont で FontAwesome を指定し、QPainter 使って QPixmap にアイコンを書き出す
QPainter painter(&pixmap);
QFont font("FontAwesome", fontPointSize);
painter.setFont(font);
painter.setRenderingHint(QPainter::AntiAliasing);
painter.drawText(pixmap.rect(), iconType, QTextOption(Qt::AlignVCenter|Qt::AlignHCenter));

// QPixmap から QIcon に変換
QIcon icon(pixmap);

// ... icon を使ってゴニョゴニョする

// 終わったら removeApplicationFont で削除
QFontDatabase::removeApplicationFont(fontID)

FontAwesome を使用していてより柔軟性を求めるなら QtAwesome を使ったほうが良いかもしれません。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした