QPainter scaling for non-cosmetic pen width fails for graphicssystem raster
In my "charting/plotting widget":http://www.workslikeclockwork.com/index.php/components/qt-plotting-widget/ I scale the painter to allow saving PNGs with scaled content e.g. for high quality printing.
In the native painting mode (X11) this works as expected and also scales pen widths, however, raster seems to break this. (Which is very unfortunate, because I suggest using raster to all my users due to the massively improved performance over native X11 drawing.)
Here's a minimal example (Qt 4.7), that shows the problem:
QPixmap pm(50, 50);
painter.setRenderHint(QPainter::NonCosmeticDefaultPen); painter.scale(5, 5); painter.drawLine(0, 0, 50, 50); painter.end(); pm.save("./test.png");
So the default pen width is 0. Since we want it to not be respected as a cosmetic pen when scaling, we set the corresponding render hint.
If this is run on X11 with the native graphics system, the file test.png shows a thick diagonal line (width obviously scaled). When I provide -graphicssystem raster as command line argument, the line is only one pixel wide.
!http://www.workslikeclockwork.com/graphics/native.png(resulting image, rendered with native graphicssystem)! !http://www.workslikeclockwork.com/graphics/raster.png(resulting image, rendered with raster graphicssystem)!
Now here are my questions regarding this:
- Is this a bug in Qt or is it actually expected behaviour? (If expected, where in the doc does it say so?)
- Does anyone know whether this is different/fixed in Qt 4.8? I only have Qt 4.7 here.
- Is it possible to set a different graphicssystem for painting into a QPixmap than the one that's currently being used to display the widget in the GUI?
- Is there a way to determine the currently active graphicssystem so I could work around the problem by manually scaling the pen width?
In 4.8 I still get a cosmetic pen, so perhaps it's a bug.
But... if you do want the pen to scale, why don't you just set a width of 1?
[quote]Is it possible to set a different graphicssystem for painting into a QPixmap than the one that’s currently being used to display the widget in the GUI?[/quote]
I don't think so, the point of the graphicsssytem is to choose the paint engine that paints on QWidgets and QPixmaps.
[quote]Is there a way to determine the currently active graphicssystem so I could work around the problem by manually scaling the pen width?[/quote]
Not sure if QApplication has an accessor for this, you could simply check the paint engine type on a QPixmap (yes, it's a huge hack. :-))
bq. But… if you do want the pen to scale, why don’t you just set a width of 1?
Since I also export to vector formats like PDF I do need the option of cosmetic pens, so most of the pens indeed need width 0. Setting them to width 1 for the scaled PNG export would cause equally much work and code-clutter as scaling the widths myself. So this would only be my last resort...