Skip to main content
`Ctrl`+`K`

# `PrintPreview`: Print preview dialog

This module implements a print preview dialog.

The dialog provides methods to send images, pixmaps and SVG items to the page to be printed.

The user can interactively move and resize the items.

## Widgets

class PrintPreviewDialog(parent=None, printer=None)

  Bases: `QDialog`

  Print preview dialog widget.

  addImage(image, title=None, comment=None, commentPosition=None)

    Add an image to the print preview scene.

    Parameters:

      - image (QImage) – Image to be added to the scene

      - title (str) – Title shown above (centered) the image

      - comment (str) – Comment displayed below the image

      - commentPosition – “CENTER” or “LEFT”

  addPixmap(pixmap, title=None, comment=None, commentPosition=None)

    Add a pixmap to the print preview scene

    Parameters:

      - pixmap (QPixmap) – Pixmap to be added to the scene

      - title (str) – Title shown above (centered) the pixmap

      - comment (str) – Comment displayed below the pixmap

      - commentPosition – “CENTER” or “LEFT”

  addSvgItem(item, title=None, comment=None, commentPosition=None, viewBox=None, keepRatio=True)

    Add a SVG item to the scene.

    Parameters:

      - item (QSvgRenderer) – SVG item to be added to the scene.

      - title (str) – Title shown above (centered) the SVG item.

      - comment (str) – Comment displayed below the SVG item.

      - commentPosition (str) – “CENTER” or “LEFT”

      - viewBox (QRectF) – Bounding box for the item on the print page (xOffset, yOffset, width, height). If None, use original item size.

      - keepRatio (bool) – If True, resizing the item will preserve its original aspect ratio.

  setup()

    Open a print dialog to ensure the `printer` is set.

    If the setting fails or is cancelled, `printer` is reset to None.

  ensurePrinterIsSet()

    If the printer is not already set, try to interactively setup the printer using a QPrintDialog. In case of failure, hide widget and log a warning.

    Returns:

      True if printer was set. False if it failed or if the selection dialog was canceled.

  setOutputFileName(name)

    Set output filename.

    Setting a non-empty name enables printing to file.

    Parameters:

      name (str) – File name (path)

  exec(self) → int
  exec_(self) → int
  raise_(self)

class SingletonPrintPreviewDialog(*var, **kw)

  Bases: `PrintPreviewDialog`

  Singleton print preview dialog.

  All widgets in a program that instantiate this class will share a single print preview dialog. This enables sending multiple images to a single page to be printed.

## Example

    import sys
    from silx.gui import qt
    from silx.gui.widgets import PrintPreviewDialog

    a = qt.QApplication(sys.argv)

    if len(sys.argv) < 2:
        print("give an image file as parameter please.")
        sys.exit(1)

    if len(sys.argv) > 2:
        print("only one parameter please.")
        sys.exit(1)

    filename = sys.argv[1]
    w = PrintPreviewDialog()
    w.resize(400, 500)

    comment = ""
    for i in range(20):
        comment += "Line number %d: En un lugar de La Mancha de cuyo nombre ...\n"

    if filename[-3:] == "svg":
        item = qt.QSvgRenderer(filename, w.page)
        w.addSvgItem(item, title=filename,
                     comment=comment, commentPosition="CENTER")
    else:
        w.addPixmap(qt.QPixmap.fromImage(qt.QImage(filename)),
                    title=filename,
                    comment=comment,
                    commentPosition="CENTER")
        w.addImage(qt.QImage(filename), comment=comment, commentPosition="LEFT")

    w.exec()
    a.exec()