QIcon display part of image , possible?
Question
I wanted to use a single image for all of my button images , and when displaying icons , i wanted to display only desired range of that image , is it possible ?
Thanks !
Solution
You can create the icons from your 'sprite sheet' by reading it as a QImage and painting it into another QImage:
QIcon GetIcon(left, top, width, height) // or calculate these from an icon index or such
{
QImage sprite, result;
sprite = QImage(pathToSprite);
result = QImage(resultingIconSize, theQImageFormat);
QPainter painter(&result);
painter.drawImage(0, 0, sprite, left, top, width, height);
painter.end();
return QIcon(QPixmap::fromImage(result));
}
By setting the composition mode you can even overlay several images with transparency.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow