#include <QCoreApplication>
#include <QDebug>
#include "qcustomplot.h"
#include <QPixmap>
#include <QByteArray>
#include <QBuffer>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 创建 QCustomPlot 对象并绘制图形
QCustomPlot customPlot;
customPlot.addGraph();
QVector<double> xData = {1, 2, 3, 4, 5};
QVector<double> yData = {1, 4, 9, 16, 25};
customPlot.graph(0)->setData(xData, yData);
customPlot.rescaleAxes();
customPlot.replot();
// 创建 QPixmap 对象并将 QCustomPlot 渲染到 QPixmap 上
QPixmap pixmap(customPlot.size());
customPlot.render(&pixmap);
// 创建 QByteArray 对象并将 QPixmap 写入 QByteArray 中
QByteArray byteArray;
QBuffer buffer(&byteArray);
buffer.open(QIODevice::WriteOnly);
pixmap.save(&buffer, "PNG"); // 可以选择其他图像格式,如 "JPG" 或 "BMP"
buffer.close();
qDebug() << "Image saved to QByteArray.";
return a.exec();
}
QCustomPlot保存图片
最新推荐文章于 2024-04-29 21:03:23 发布