I want to save picture from QGraphicsScene. the picture just like using subplot
in Python with Matplotlib.
In addition, I cannot use function QGraphicsView view.show()
because my teacher ask for it.
The following is my code. when I use view.show()
to check it, it show nothing, I do not know where is wrong.
#include <QApplication>
#include <QtWidgets>
#include <QtCharts>
#include <QPrinter>
QT_CHARTS_USE_NAMESPACE
static bool create_series(QLineSeries *adc_series, QLineSeries *temp_series)
{
for(int i=0; i<1000; i++)
{
adc_series->append( i, 30 );
temp_series->append(i, 80 );
}
return true;
}
static QChart* create_chart(const QString & title, QLineSeries *series)
{
QChart * chart = new QChart;
chart->legend()->hide();
chart->addSeries(series);
QDateTimeAxis *axisX = new QDateTimeAxis;
axisX->setTickCount(10);
axisX->setFormat("hh:mm:ss");
axisX->setTitleText("Time Axis");
chart->addAxis(axisX, Qt::AlignBottom);
series->attachAxis(axisX);
QValueAxis *adc_axisY = new QValueAxis;
adc_axisY->setLabelFormat("%i");
adc_axisY->setTitleText(title);
adc_axisY->setRange(0, 100);
chart->addAxis(adc_axisY, Qt::AlignLeft);
series->attachAxis(adc_axisY);
return chart;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLineSeries *adc_series = new QLineSeries;
QLineSeries *temp_series = new QLineSeries;
if(!create_series(adc_series, temp_series)) return -1;
QGraphicsScene scene;
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
layout->addItem(create_chart("ADC Value", adc_series));
layout->addItem(create_chart("Temperature Value", temp_series));
layout->setSpacing(0);
QGraphicsWidget *form = new QGraphicsWidget;
form-> setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
form->setLayout(layout);
scene.addItem(form);
// QGraphicsView view(&scene);
// view.show();
scene.clearSelection();
scene.setSceneRect(scene.itemsBoundingRect());
QImage image(scene.sceneRect().size().toSize(), QImage::Format_ARGB32);
image.fill(Qt::transparent);
QPainter painter(&image);
scene.render(&painter);
image.save("new.png");
return a.exec();
}
question from:
https://stackoverflow.com/questions/65831688/how-to-create-image-file-from-qgraphicsscene 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…