Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to export QGraphicsScene to QImage with HiDPI



  • I have following function that exports my QGraphicsScene to a QImage:

    QImage MyGraphicsScene::image()
    {
    	auto scaleFactor = mDevicePixelRatioScaler->scaleFactor(); // Get the current scale factor in the example it was 2 for 200% scaling
    	QImage image(sceneRect().size().toSize() * scaleFactor, QImage::Format_ARGB32_Premultiplied);
    	image.fill(Qt::white);
    
    	QPainter painter(&image);
    	painter.setRenderHint(QPainter::Antialiasing);
    	render(&painter);
            image.setDevicePixelRatio(scaleFactor);
    
    	return image;
    }
    

    It works fine without HiDPI but with HiDPI enabled, like here set to 200% the exported image looses quality and it looks like this:

    alt text

    The original image looks like this:

    alt text

    Without the scale factor multiplying there the exported images would be small and look like there was no scaling at all so in order to get the size right I have to upscale it but as you can see, I lose quality.

    Does anyone know how to do it right?



  • Ok, it looks like the fix was as simple as setting this before creating the QApplication

    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    


  • Ok, it looks like the fix was as simple as setting this before creating the QApplication

    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    

Log in to reply