user7431005
user7431005

Reputation: 4539

qt render chart does not update title/legend

I'm trying to render a QChartView into a .png image using the following simplyfied code:

QChartView *chartView = /* pointer to chart view */;
const auto dpr = chartView->devicePixelRatioF();
chartView->setMinimumSize(500, 500);
chartView->setMaximumSize(500, 500);
QImage image(500, 500, QImage::Format_ARGB32_Premultiplied);
image.setDevicePixelRatio(dpr);
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
chartView->render(&painter);
image.save('image.png');

It works fine and it saves an image as intended. However, if I try to change the title before rendering the title is not shown:

QChartView *chartView = /* pointer to chart view */;
const auto dpr = chartView->devicePixelRatioF();
chartView->setMinimumSize(500, 500);
chartView->setMaximumSize(500, 500);
chartView->chart()->setTitle("my title"); // <--- HERE
QImage image(500, 500, QImage::Format_ARGB32_Premultiplied);
image.setDevicePixelRatio(dpr);
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
chartView->render(&painter);
image.save('image.png');

However, if I create a button which displays a title, and a button which should save the image and I first click the button to show the title and then the button to save the image it works. (Meaning, if I separate the two steps, so that in between the chart is displayed in the GUI it works)

The same is true when I try to show a legend.

Update: I have added a complete minimal example to reproduce the problem:

#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>

QT_CHARTS_USE_NAMESPACE

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QLineSeries *series = new QLineSeries();
    series->setName("label");

    series->append(0, 6);
    series->append(2, 4);
    series->append(3, 8);
    series->append(7, 4);
    series->append(10, 5);
    *series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);

    QChart *chart = new QChart();
    chart->addSeries(series);
    chart->createDefaultAxes();

    QChartView *chartView = new QChartView(chart);
    chartView->setRenderHint(QPainter::Antialiasing);

    chart->setTitle("");
    chart->legend()->hide();
    chartView->grab().toImage().save("noLabels.png");

    chart->setTitle("title");
    chart->legend()->show();
    chartView->grab().toImage().save("withLabels.png");

    QMainWindow window;
    window.setCentralWidget(chartView);
    window.resize(400, 300);
    window.show();

    return a.exec();
}

both created images noLabels.png and withLabels.png do not show a title or a legend.

Upvotes: 0

Views: 1125

Answers (2)

Gasteizko
Gasteizko

Reputation: 208

Did you tried to use repaint() before setting the title?

Upvotes: 0

Nejat
Nejat

Reputation: 32645

It seems that the events are queued and processed later when you set title or show legend. You can force them to take place by resizing the chart view after setting title and showing legend:

chart->setTitle("title");
chart->legend()->show();
chartView->resize(QSize(500,500));
chartView->grab().toImage().save("withLabels.png");

Upvotes: 1

Related Questions