Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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)) {
    painter.setFont(QFont("Arial",12));
    painter.drawText(0,8,"ABCDEFGHIJK");
    painter.drawText(0,24,"01234567890");
    painter.end();
    }
    }

    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?

    0_1544787563052_20181214_121137.jpg



  • 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

    Hi,

    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()
    {

    Q_Q(QPrintPreviewWidget);
    printer->d_func()->setPreviewMode(true);
    emit q->paintRequested(printer);
    printer->d_func()->setPreviewMode(false);
    

    .......

    void QPrinterPrivate::setPreviewMode(bool enable)
    {

    Q_Q(QPrinter);
    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