Qt 旋转图像时,如何用透明背景填充空白?
当我在Qt中旋转一个图像时,它会自动放大图像,以封装更大的尺寸,这是伟大和智能的。然而,它用白色填充了新创建的空白,这不是我想要的。我怎样才能使它充满一个完全透明的颜色 这是梅的密码Qt 旋转图像时,如何用透明背景填充空白?,qt,qtgui,Qt,Qtgui,当我在Qt中旋转一个图像时,它会自动放大图像,以封装更大的尺寸,这是伟大和智能的。然而,它用白色填充了新创建的空白,这不是我想要的。我怎样才能使它充满一个完全透明的颜色 这是梅的密码 QImage lSource("/path/to/an/image/file"); QTransform lRotation; lRotation.rotate(30.0); QImage lRotated(lSource.transformed(lRotation, Qt::SmoothTransformatio
QImage lSource("/path/to/an/image/file");
QTransform lRotation;
lRotation.rotate(30.0);
QImage lRotated(lSource.transformed(lRotation, Qt::SmoothTransformation));
ui.labelImage->setPixmap(QPixmap::fromImage(lRotated));
非常感谢 根据我上面的评论
我怀疑,旋转图像的空白区域是透明的,而白色是标签的背景色。如果要为旋转图像设置特定背景,可以执行以下操作:
// Create rotated image.
QImage lSource("source.png");
QTransform lRotation;
lRotation.rotate(30.0);
QImage lRotated(lSource.transformed(lRotation, Qt::SmoothTransformation));
// Create resulting image with specific background.
QImage img(lRotated.size(), QImage::Format_ARGB32);
img.fill(Qt::red); // Set the red background
QPainter p(&img);
p.drawImage(0, 0, lRotated);
QLabel l;
l.setPixmap(QPixmap::fromImage(img));
l.show();
我怀疑,旋转图像的空白区域是透明的,而白色是标签的背景色。您希望在那里看到什么颜色?谢谢您的及时回复!你说得对。这是标签的背景。我的错。谢谢你详细的回答!是的,是我的错。我通过简单地添加以下代码修复了这个问题:ui.labelImage->setBackgroundRole(qpalete::Dark);由于它被用作QSCROLLLAREA中的小部件,因此它与QScroller区域的背景完全匹配。