user7431005
user7431005

Reputation: 4539

render QChartView with high resolution

I want to save a QChartView as a .png image. Therefore I use the following code:

QChartView *chartView = qobject_cast<QChartView*>(/* get chart view */);

QImage image;
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
chartView->render(&painter);
image.save("test.png");

When I look at the test.png image the resolution is quite bad.

Can I somehow say, that the QChartView should be rendered with a fixed resolution like 150dpi or a fixed size like (500x700 pixels)?

Upvotes: 0

Views: 1799

Answers (1)

mohabouje
mohabouje

Reputation: 4050

You can scale your image by using QPaintDevice::devicePixelRatioF() and using a QPixmap instead of an image.

const auto dpr = chartView->devicePixelRatioF();
QPixmap buffer(chartView->width() * dpr, chartView->height() * dpr);
buffer.setDevicePixelRatio(dpr);
buffer.fill(Qt::transparent);

QPainter *paint = new QPainter(&buffer);
paint->setPen(*(new QColor(255,34,255,255)));
chartView->render(paint);

Once the image is scaled to the proper resolution, you can convert it to a QImage or save it directly:

QFile file("image.png");
file.open(QIODevice::WriteOnly);
uffer.save(&file, "PNG");

Upvotes: 1

Related Questions