Plot Export - QwtPlotRenderer¶
QwtPlotRenderer provides functionality to export plots to various file formats, supporting image files (PNG, JPG, etc.), PDF, SVG vector formats, and print output.
Main Features¶
- Export to PNG, JPG, BMP, PDF, SVG, PS, and other formats
- Configurable DPI resolution for exported images
- Custom export dimensions, including millimeter units
- Titles, legends, and other components optionally ignored during export
Usage¶
1. Export to Image File¶
All file exports go through QwtPlotRenderer::renderDocument(): you pass the plot, the target filename (whose extension selects the format), the output size in millimeters (QSizeF), and the resolution in DPI (default 85). For image formats the pixel dimensions are derived as mm / 25.4 * DPI. The example below writes the same plot as PNG, JPG, and BMP:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
2. Export to PDF¶
PDF output uses the same renderDocument() entry point; just give the file a .pdf extension. The result is a vector document that stays sharp at any zoom level, ideal for printing and document embedding:
1 2 3 4 | |
3. Export to SVG¶
With a .svg filename, renderDocument() produces a scalable vector graphic (Qwt must be built with SVG support, QWT_CONFIG_QWTSVG). SVG output remains editable and can be embedded directly into web pages:
1 2 3 4 5 6 7 | |
4. Export to QImage/QPixmap¶
Instead of writing a file, the renderer can also draw into an in-memory paint device: the renderTo(QwtPlot*, QPaintDevice&) overload accepts any QImage or QPixmap that you create yourself at the desired pixel size, which is handy for embedding the result in an application or post-processing it:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
5. Export Options¶
The DiscardFlag enum strips selected components from the output: DiscardBackground, DiscardTitle, DiscardLegend, DiscardCanvasBackground, DiscardFooter, and DiscardCanvasFrame. Single flags are toggled with setDiscardFlag(), while setDiscardFlags() assigns the whole set at once:
1 2 3 4 5 6 7 8 9 10 11 | |
6. Print Function¶
For printing, a configured QPrinter is handed to the renderer (renderTo(QwtPlot*, QPrinter&)), which lays the plot out across the page. The printer itself decides the target: a physical device or, as below, a PDF file:
1 2 3 4 5 6 7 8 9 10 | |
7. Using Millimeter Units for Size¶
The size argument of renderDocument() is a QSizeF interpreted in millimeters, so the exported document has well-defined physical dimensions; the resolution argument (default 85) sets the dots per inch used when rasterizing content:
1 2 3 | |
Export Format Comparison¶
| Format | Advantages | Disadvantages | Use Cases |
|---|---|---|---|
| PNG | Lossless compression, transparency support | Larger file size | Web sharing, screenshots |
| JPG | Small file size, widely supported | Lossy compression, no transparency | Photo-like charts |
| Vector, high resolution | Requires PDF reader | Printing, documents | |
| SVG | Vector, editable | Complex graphics render slowly | Web pages, editing |
| BMP | Lossless, simple | Very large file size | Specific software |
Export Option Enum¶
| Option | Description |
|---|---|
DiscardBackground |
Ignore the plot background |
DiscardTitle |
Ignore title |
DiscardLegend |
Ignore legend |
DiscardFooter |
Ignore footer |
DiscardCanvasBackground |
Ignore canvas background |
DiscardCanvasFrame |
Ignore the canvas frame (no effect with style sheets, where the frame is part of the background) |
Core Method Summary¶
| Method | Description |
|---|---|
renderDocument() |
Export to a file (format chosen by extension or passed explicitly) |
renderTo() |
Render into a QPaintDevice (QImage/QPixmap), a QPrinter or a QSvgGenerator |
exportTo() |
Open a file dialog and export to the selected file |
setDiscardFlag() / setDiscardFlags() |
Set/reset discard options (DiscardNone renders everything) |
Tip β Export Recommendations
- Use PDF or high-resolution PNG (300 DPI) for printing
- Use SVG or PNG for web pages
- Use PDF or SVG for document embedding
- Use PNG or JPG for screenshot sharing
Example β Related Examples
- Export functionality demo:
examples/2D/barchart