Qt之生成png/jpg/bmp格式图片_qt生成图片-CSDN博客
(1)使用QPainter
示例关键代码:
QImage image(QSize(this->width(),this->height()),QImage::Format_ARGB32);
image.fill("white");
QPainter *painter = new QPainter(&image);
painter->save();
QPen pen;
pen.setWidth(2);
pen.setColor(Qt::red);
painter->setPen(pen);
painter->drawEllipse(QPoint(width()/2,height()/2),50,50);
painter->drawLine(QPointF(0,0),QPointF(width()/2,height()/2));
painter->drawRect(QRect(40,40,150,160));
painter->restore();
painter->end();
QString path=QApplication::applicationDirPath()+"/1.png";
qDebug()<<path;
image.save(path);
再提取:
QImage image(QSize(this->width(),this->height()),QImage::Format_ARGB32);
image.fill("white");
QPainter *painter = new QPainter(&image);
painter->save();
//使用QPainter进行绘制
//...
painter->end();
//保存为图片
QString path=QApplication::applicationDirPath()+"/1.png";
image.save(path);
就是在QImage上绘制图像,再保存为图片。
(2)直接把QWidget窗体上的内容保存为图片。
示例代码:
Widget w;
w.show();
QPixmap pixmap(w.size());
w.render(&pixmap);
QString path=QApplication::applicationDirPath()+"/3.png";
pixmap.save(path);
参考:
保存Qwiget 图像 并调整大小-QT开发中文网 (0voice.com)