使用Qt生成图片

Qt之生成png/jpg/bmp格式图片_qt生成图片-CSDN博客

(1)使用QPainter

示例关键代码:

cpp 复制代码
    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);

再提取:

cpp 复制代码
    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窗体上的内容保存为图片。

示例代码:

cpp 复制代码
    Widget w;
    w.show();
    QPixmap pixmap(w.size());
    w.render(&pixmap);
    QString path=QApplication::applicationDirPath()+"/3.png";
    pixmap.save(path);

参考:

保存Qwiget 图像 并调整大小-QT开发中文网 (0voice.com)

相关推荐
阿闽ooo12 分钟前
单例模式深度解析:从饿汉到懒汉的实战演进
开发语言·c++·笔记·设计模式
爱喝水的鱼丶12 分钟前
SAP-ABAP:通过接口创建生产订单报“没有工艺路线选中”错误解决办法详解
运维·开发语言·sap·abap·bapi·生产订单
x70x8017 分钟前
C++中auto的使用
开发语言·数据结构·c++·算法·深度优先
Han.miracle20 分钟前
数据结构与算法-012
java·开发语言
智航GIS28 分钟前
2.1 变量与数据类型
开发语言·python
拼好饭和她皆失34 分钟前
c++---快速记忆stl容器
开发语言·c++
黎雁·泠崖38 分钟前
C 语言字符串高阶:strstr/strtok/strerror 精讲(含 strstr 模拟实现)
c语言·开发语言
PeaceKeeper739 分钟前
Objective-c的内存管理以及Block
开发语言·macos·objective-c
2501_9369603640 分钟前
c语言期末速成8——文件
c语言·开发语言