Drawing a scalable QIcon using QPainter

Johannes Schaub - litb picture Johannes Schaub - litb · Sep 17, 2012 · Viewed 8.5k times · Source

I want to design a new QIcon and want it to look like a fixed text with a rounded rectangle around it

.-----.
| Phy |
`-----´

The icon is supposed to scale without the "pixel-blocks" effect when painting on a QPainter that eventually has a scale transformation applied (for example when I paint into a widget that is part of a QGraphicsView with a scale applied on its scene).

Therefor, I have difficulties knowing how I should paint my QIcon. If I do it in the following way, I will paint a QPixmap that always has a fixed amount of pixels, thus introducing the pixel-blocks effect inevitably when the scale is large enough

 void MyWidget::drawIcon(QPainter *painter, QPoint pos) {
     QPixmap pixmap = icon.pixmap(QSize(22, 22),
                                    isEnabled() ? QIcon::Normal
                                                : QIcon::Disabled,
                                    isChecked() ? QIcon::On
                                                : QIcon::Off);
     painter->drawPixmap(pos, pixmap);
 }

What I am looking for is a way similar to how QFont with drawText works. Regardless on how large my scale is, when I draw fonts it always looks sharp and I cannot detect individual pixels.

I imagine that I could tell QPainter to paint my icon into a given pixel rectangle, and QPainter transforms the rectangle itself before letting my QIconEngine::paint render the item into a possibly larger rectangle or pixmap. But I see no way how I could do something like this.

Am I just being stupid and not seeing the obvious solution?

Answer

Johannes Schaub - litb picture Johannes Schaub - litb · Sep 17, 2012

I was indeed completely dump. I can just use QIcon::paint and pass it the rectangle. It will correctly delegate the request to the icon engine.