In the code that modifies the QImage pixel, check first to see if the alpha channel is zero. If it is, do not modify the pixel. And when setting the pixel, be sure to copy the alpha channel, not just RGB.
{
// Load the QImage directly from the resource. No need to create a QPixmap first
QImage tmp
( ":/Images/PlayerMoveDownStill.png",
"PNG" );
for(int i=0;i<tmp.height();i++)
{
for(int k=0;k<tmp.width();k++)
{
QColor color
( tmp.
pixel( k, i
) );
// changed this from pixelColor() to ensure alpha is copied if ( color.alpha() != 0 ) // modify only the pixels with non-zero alpha
{
color.setRgb(color.red()*0.5,color.blue()*0.5,color.green()*0.5, color.alpha() );
tmp.setPixelColor(k,i,color);
}
}
}
// Now, convert the image to a pixmap and set it on the graphics object
setPixmap( t );
setPos(game->width()/2-this->boundingRect().width()/2,game->height()/2-this->boundingRect().height()/2);
}
Player::Player(QGraphicsPixmapItem *parent)
{
// Load the QImage directly from the resource. No need to create a QPixmap first
QImage tmp( ":/Images/PlayerMoveDownStill.png", "PNG" );
for(int i=0;i<tmp.height();i++)
{
for(int k=0;k<tmp.width();k++)
{
QColor color( tmp.pixel( k, i ) ); // changed this from pixelColor() to ensure alpha is copied
if ( color.alpha() != 0 ) // modify only the pixels with non-zero alpha
{
color.setRgb(color.red()*0.5,color.blue()*0.5,color.green()*0.5, color.alpha() );
tmp.setPixelColor(k,i,color);
}
}
}
// Now, convert the image to a pixmap and set it on the graphics object
QPixmap t = QPixmap::fromImage( tmp );
setPixmap( t );
setPos(game->width()/2-this->boundingRect().width()/2,game->height()/2-this->boundingRect().height()/2);
}
To copy to clipboard, switch view to plain text mode
Bookmarks