Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPrinter: The output becomes stretched
Forum Updated to NodeBB v4.3 + New Features

QPrinter: The output becomes stretched

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 889 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mads-lM Offline
    mads-lM Offline
    mads-l
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sifourquier
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • mads-lM Offline
        mads-lM Offline
        mads-l
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          What are the differences ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • mads-lM Offline
            mads-lM Offline
            mads-l
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • annajulia1A Offline
              annajulia1A Offline
              annajulia1
              Banned
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved