First, create a 32×32 QImage and make it transparent (or whatever background color you need):
QImage img(32, 32, QImage::Format_ARGB32); img.fill(QColor(0, 0, 0, 0));
I use QImage instead of QPixmap, because the documentation says that QImage is optimized for I/O, e.g. painting, and the first attempts with QPixmap looked like crap. Next, create a QPainter for the QPaintDevice and set some render hints to make it look nice:
QPainter *p = new QPainter(&img); p->setRenderHint(QPainter::Antialiasing); p->setRenderHint(QPainter::TextAntialiasing); p->setRenderHint(QPainter::SmoothPixmapTransform);
QPainter::setBrush() sets the background color for shapes, QPainter::setPen() the foreground color for text:
p->setBrush(QColor(Qt::red)); p->setPen(QColor(Qt::white));
Then select a font the size of our future Icon:
QFont f("courier new"); f.setPixelSize(32); p->setFont(f);
Now we need some background. White on transparent isn’t really readable, so let’s draw a circle:
p->drawEllipse(img.rect());
Since our QImage is actually a square and not an rectangle, QPainter::drawEllipse will draw a circle. Print the QChar, letter, or whatever:
p->drawText(img.rect(), Qt::AlignCenter, QChar(char));
Now clean up and return the QImage as QIcon:
delete p; return QIcon(QPixmap::fromImage(img));
The whole shebang can be marveled at here. It looks like this:
Have fun!