延续前面的话题,继续讨论绘图。
这里使用QPixmap绘图设备,不是在paintEvent中使用的,而是直接在Widget的构造函数中使用。
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//绘图设备,大小为400*300
QPixmap pixmap(400, 300);
QPainter p(&pixmap);
//填充背景
//p.fillRect(0, 0, 400, 300, QBrush(Qt::white));
pixmap.fill(Qt::white);
p.drawPixmap(0, 0, 80, 80, QPixmap("../Image/noya.jpg"));
//保存图片
pixmap.save("../pixmap.jpg");
}
save方法会将图片保存到指定路径:
在手动设置填充背景之前:
可以看出,背景是灰色的,但是在手动填充背景色为白色之后:
成了。
整理自B站网课,侵删歉。