QIcon显示部分图像,可能吗?

时间:2012-02-21 12:08:48

标签: icons paint qt4.7

我想为我的所有按钮图像使用单个图像,当显示图标时,我想只显示该图像的所需范围,是否可能?

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以通过将其作为QImage读取并将其绘制到另一个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));
}

通过设置composition mode,您甚至可以使用透明度覆盖多个图像。