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

问题描述:

我想为我的所有按钮图像使用单个图像,并且在显示图标时,我只想显示该图像的所需范围,可以吗?QIcon显示图像的一部分,可能吗?

谢谢!通过设置composition mode你甚至可以覆盖多个图像的透明度

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)); 
} 

您可以通过阅读它作为一个QImage的,它画到另一个QImage的创建你的“精灵表”的图标。