Important: Please read the Qt Code of Conduct -

QPrinter: The output becomes stretched

  • Hello,

    My question is: How can I make the printout correct without using the QPrintPreviewDialog?

    I am new to QT printing support,working with a label printer. The OS is windows 7 and in the printer preferences the page height is 12 mm and width 40 mm. The label is drawn correctly in the Preview Dialog (QPrintPreviewDialog class) and prints correctly when print is activated with the print button in the preview. The printPreview slot called from the preview dialog looks like this:

    void MainWindow::printPreview(QPrinter *printer) {
    QPainter painter;
    if (painter.begin(printer)) {

    However, there should be no interaction during the printing, so instead of activating the QPrintPreviewDialog with my QPrinter object, I did a test calling the above code directly with a pointer to the QPrinter. In this case the printout becomes stretched, and the width of the document is not respected. The stretching factor depends on the width set in the printer driver. (See picture.)
    What can I do to print correctly?


  • have you test to use setPageSize()?
    else i think you need post the end of your code for people can help you

  • It looks like the QPrinter passed to the printPreview slot has a different QPaintEngine compared to the QPaintEngine created when QPrinter was constructed. Looking in the QT source, a new previewpaintengine seems to be swapped in just while the printPreview slot is activated, and immediately restored after.
    What can I do to instruct the original QPaintEngine to print correctly?

  • Lifetime Qt Champion


    What are the differences ?

  • The difference in the printer output is attatched to the first posting, I am not able to see the big picture in the printer support classes, but I noticed that something happend to the QPaintEngine before the user written slot actually doing the drawing on the QPrinter in is called:

    void QPrintPreviewWidgetPrivate::generatePreview()

    emit q->paintRequested(printer);


    void QPrinterPrivate::setPreviewMode(bool enable)

    if (enable) {
        if (!previewEngine)
            previewEngine = new QPreviewPaintEngine;
        had_default_engines = use_default_engine;
        use_default_engine = false;
        realPrintEngine = printEngine;
        realPaintEngine = paintEngine;
        q->setEngines(previewEngine, previewEngine);
        previewEngine->setProxyEngines(realPrintEngine, realPaintEngine);
    } else {
        q->setEngines(realPrintEngine, realPaintEngine);
        use_default_engine = had_default_engines;


    Based on this, it seems that the correct drawing requires the paint engine used by the preview class,.However how can I achive this without the preview, using the default paint engine?

  • Banned

    This post is deleted!

Log in to reply