Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

c++ - How to create image file from QGraphicsScene?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...