Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. QPrint no possible to set page size and orientation (ubuntu 18.04)
QtWS25 Last Chance

QPrint no possible to set page size and orientation (ubuntu 18.04)

Scheduled Pinned Locked Moved Unsolved C++ Gurus
11 Posts 4 Posters 3.7k Views
  • 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.
  • G Offline
    G Offline
    gfxx
    wrote on 11 Apr 2019, 17:44 last edited by gfxx 4 Nov 2019, 17:44
    #1

    Hi at all ... these my code:

        QPrinter printer;
        printer.setPaperSize(QPrinter::A4);  //(QSizeF(210, 297), QPrinter::Millimeter);
        printer.setOrientation(QPrinter::Landscape);
        printer.setPageMargins(5, 5, 5, 5, QPrinter::Millimeter);
        printer.setColorMode(QPrinter::Color);
        printer.setResolution(600);
        printer.setFullPage(false);
    
        printer.setOutputFileName(resultString);
        printer.setOutputFormat(QPrinter::PdfFormat);
    
        qDebug() << "The new doc Layout is:   " << printer.pageLayout();  /* debug show correct layout */
    
        QImage img = qvariant_cast<QImage>(data);
        QImage imgS = img.scaled(7200, 3550, Qt::AspectRatioMode::KeepAspectRatioByExpanding);
    
       QPainter painter(&printer);
       painter.drawImage(QPoint(0,0),imgS);
       painter.end();
    
    
    

    I can print the image.png , and image it is ok ... all part of it is correct, but if I use these code for print to pdf, pdf is print only at default layout and page size -> US Letter, vertical (216 × 279 mm), format is PDF-1.4.

    Is not a system problem because i can print in every way with other application istalled (inskape etc etc) .... the image looks correct, it looks in landscape mode and the correct size, but on the pdf file it is printed only halfway because the paper size and orientation are wrong, even if the debug shows the right settings.

    Can someone direct me in the right direction to solve the problem?

    regards

    bkt

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 Apr 2019, 19:50 last edited by
      #2

      hi,

      Did you try first setting the output format and then the other parameters ?

      By the way, since you are writing PDF files, you might want to check QPdfWriter.

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

      G 1 Reply Last reply 12 Apr 2019, 05:33
      1
      • S SGaist
        11 Apr 2019, 19:50

        hi,

        Did you try first setting the output format and then the other parameters ?

        By the way, since you are writing PDF files, you might want to check QPdfWriter.

        G Offline
        G Offline
        gfxx
        wrote on 12 Apr 2019, 05:33 last edited by gfxx 4 Dec 2019, 06:57
        #3

        @SGaist said in QPrint no possible to set page size and orientation (ubuntu 18.04):

        Did you try first setting the output format and then the other parameters ?

        Yes, nothing changes ...... it seems that the settings are valid for the printed object but not for the paper on which it is printed .... I see the printed object oriented in landscape or in potrait if I change the setting, I see the resolution change, if I change the setting, I see the print size change if I change setpageSize, but the card always remains the same in every case: US Letter, vertical (216 × 279 mm).

        I'm frustrated ... I've been trying to get around this problem for 8 hours ...

        About QPdfWriter .... if I make a simple instance to QPdfWriter writer .... it doesn't change anything ....

        ---------UPDATE-------------------

        I try to renew the code to my original one, in these manner:

            QPrinter printer;
            printer.setPaperSize(QPrinter::A4);  //(QSizeF(210, 297), QPrinter::Millimeter);
            printer.setOrientation(QPrinter::Landscape);
            printer.setPageMargins(5, 5, 5, 5, QPrinter::Millimeter);
            printer.setColorMode(QPrinter::Color);
            printer.setResolution(600);
            printer.setFullPage(false);
        
            printer.setOutputFileName(resultString);
            printer.setOutputFormat(QPrinter::PdfFormat);
        
            qDebug() << "The new doc Layout is:   " << printer.pageLayout();  /* debug show correct layout */
        
        
            QImage img = qvariant_cast<QImage>(data);
            QImage imgS = img.scaled(7200, 3550, Qt::AspectRatioMode::KeepAspectRatioByExpanding);
        
                  QPrintDialog *dlg = new QPrintDialog(&printer,nullptr);
                  dlg->setOption(QAbstractPrintDialog::PrintToFile, true);
                  if(dlg->exec() == QDialog::Accepted) {
                          QPainter painter(&printer);
                          painter.drawImage(QPoint(0,0),imgS);
                          painter.end();
        
                  }
        
        
        }
        

        when the dialog box starts, I see the perfectly set printing options (A4 format, landscape, color, margins etc etc). I start printing and ... once again I get printing in US Letter, vertical (216 × 279 mm) that is very different fron that dialog show!!

        regards

        bkt

        J 1 Reply Last reply 12 Apr 2019, 07:27
        0
        • G gfxx
          12 Apr 2019, 05:33

          @SGaist said in QPrint no possible to set page size and orientation (ubuntu 18.04):

          Did you try first setting the output format and then the other parameters ?

          Yes, nothing changes ...... it seems that the settings are valid for the printed object but not for the paper on which it is printed .... I see the printed object oriented in landscape or in potrait if I change the setting, I see the resolution change, if I change the setting, I see the print size change if I change setpageSize, but the card always remains the same in every case: US Letter, vertical (216 × 279 mm).

          I'm frustrated ... I've been trying to get around this problem for 8 hours ...

          About QPdfWriter .... if I make a simple instance to QPdfWriter writer .... it doesn't change anything ....

          ---------UPDATE-------------------

          I try to renew the code to my original one, in these manner:

              QPrinter printer;
              printer.setPaperSize(QPrinter::A4);  //(QSizeF(210, 297), QPrinter::Millimeter);
              printer.setOrientation(QPrinter::Landscape);
              printer.setPageMargins(5, 5, 5, 5, QPrinter::Millimeter);
              printer.setColorMode(QPrinter::Color);
              printer.setResolution(600);
              printer.setFullPage(false);
          
              printer.setOutputFileName(resultString);
              printer.setOutputFormat(QPrinter::PdfFormat);
          
              qDebug() << "The new doc Layout is:   " << printer.pageLayout();  /* debug show correct layout */
          
          
              QImage img = qvariant_cast<QImage>(data);
              QImage imgS = img.scaled(7200, 3550, Qt::AspectRatioMode::KeepAspectRatioByExpanding);
          
                    QPrintDialog *dlg = new QPrintDialog(&printer,nullptr);
                    dlg->setOption(QAbstractPrintDialog::PrintToFile, true);
                    if(dlg->exec() == QDialog::Accepted) {
                            QPainter painter(&printer);
                            painter.drawImage(QPoint(0,0),imgS);
                            painter.end();
          
                    }
          
          
          }
          

          when the dialog box starts, I see the perfectly set printing options (A4 format, landscape, color, margins etc etc). I start printing and ... once again I get printing in US Letter, vertical (216 × 279 mm) that is very different fron that dialog show!!

          regards

          J Online
          J Online
          JonB
          wrote on 12 Apr 2019, 07:27 last edited by JonB 4 Dec 2019, 07:31
          #4

          @gfxx
          Can you print the PDF to file from that dialog? Assuming you can, you can examine it with a text editor. I presume you should see the layout/dimensions at the head of that file, what do they say? (Oh, I think your QAbstractPrintDialog::PrintToFile, true means you already do print to file? Tell us what it says about dimensions. But also, can you instead print to printer directly, does that come out wrong?) Does getting rid of printer.setFullPage(false); make any difference?

          Get rid of that large, scaled image. Try printing just Hello world. Are you saying that you always come out in US letter?

          To help yourself, you should do a bit of this detective work till you know what is or isn't necessary to show your issue.

          G 1 Reply Last reply 12 Apr 2019, 08:39
          0
          • J JonB
            12 Apr 2019, 07:27

            @gfxx
            Can you print the PDF to file from that dialog? Assuming you can, you can examine it with a text editor. I presume you should see the layout/dimensions at the head of that file, what do they say? (Oh, I think your QAbstractPrintDialog::PrintToFile, true means you already do print to file? Tell us what it says about dimensions. But also, can you instead print to printer directly, does that come out wrong?) Does getting rid of printer.setFullPage(false); make any difference?

            Get rid of that large, scaled image. Try printing just Hello world. Are you saying that you always come out in US letter?

            To help yourself, you should do a bit of this detective work till you know what is or isn't necessary to show your issue.

            G Offline
            G Offline
            gfxx
            wrote on 12 Apr 2019, 08:39 last edited by
            #5

            Are you saying that you always come out in US letter?

            @JonB these is the minor issue .... the major one is that is impossible to set A4 format horizontal .... Any how I try to explore my file with hello world print only.

            bkt

            J 1 Reply Last reply 12 Apr 2019, 08:47
            0
            • G gfxx
              12 Apr 2019, 08:39

              Are you saying that you always come out in US letter?

              @JonB these is the minor issue .... the major one is that is impossible to set A4 format horizontal .... Any how I try to explore my file with hello world print only.

              J Online
              J Online
              JonB
              wrote on 12 Apr 2019, 08:47 last edited by
              #6

              @gfxx
              What do you mean "is the minor issue"? It's precisely the problem you are reporting!

              Track down just exactly what is wrong when, e.g. "impossible to set A4 format horizontal" --- is it impossible to set A4 format generally, or only if it's horizontal? Help yourself by discovering the minimal necessary/sufficient conditions.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                gfxx
                wrote on 12 Apr 2019, 08:56 last edited by gfxx 4 Dec 2019, 08:58
                #7

                @JonB said in QPrint no possible to set page size and orientation (ubuntu 18.04):

                What do you mean "is the minor issue"? It's precisely the problem you are reporting!

                minor because I can always transform the image and adapt it to the page ....

                Track down just exactly what is wrong when, e.g. "impossible to set A4 format horizontal" --- is it impossible to set A4 format generally, or only if it's horizontal? Help yourself by discovering the minimal necessary/sufficient conditions.

                Impossible in any way ... just now i try these:

                #include "printe.h"
                #include <QPrinter>
                #include <QPainter>
                #include <QPrintDialog>
                #include <QPixmap>
                #include <QImage>
                #include <QTime>
                #include <QDate>
                #include <QDateTime>
                #include <QDebug>
                #include <QPdfWriter>
                #include <QPrintPreviewDialog>
                #include <QPrintEngine>
                #include <QPagedPaintDevice>
                #include <QPageLayout>
                #include <QPageSize>
                #include <QPaintEngine>
                #include <QPrintPreviewWidget>
                #include <QtPrintSupport>
                
                
                
                printe::printe()
                {
                
                }
                
                void printe::print(QVariant data, QString imgString)
                
                {
                    QString baseString = "/home/Desk/Doci/folder/";
                    QString resultString = "";
                
                    QDate cd = QDate::currentDate();
                    QTime ct = QTime::currentTime();
                
                    if(imgString.isEmpty()){
                        QString bDate = cd.toString()+ct.toString();
                        imgString = "DATA_"+bDate;
                        resultString = baseString+imgString+".pdf";
                    }
                    else{
                        resultString = baseString+imgString+".pdf";
                    }
                
                    qDebug() << resultString;
                
                
                    QPainter painter;
                    //painter.drawImage(QPoint(0,0),imgS);
                
                    printer = new QPrinter;
                    printer->setPdfVersion(QPrinter::PdfVersion_1_6);
                    printer->setPaperSize(QSizeF(210, 297), QPrinter::Millimeter);  //(QSizeF(210, 297), QPrinter::Millimeter);
                    printer->setPageOrientation(QPageLayout::Landscape);  /* I try with Potrait too */
                    printer->setPageMargins(5, 5, 5, 5, QPrinter::Millimeter);
                    printer->setColorMode(QPrinter::Color);
                    printer->setOutputFormat(QPrinter::PdfFormat);
                    printer->printEngine();
                    printer->setResolution(600);
                    printer->setFullPage(false);
                    printer->setOutputFileName(resultString);
                
                
                    qDebug() << "the new Layout is:   " << printer->pageLayout();
                
                    QImage img = qvariant_cast<QImage>(data);
                    QImage imgS = img.scaled(6912, 3408, Qt::AspectRatioMode::KeepAspectRatioByExpanding);
                    /*QPoint center = imgA.rect().center();
                    QMatrix matrix;
                    matrix.translate(center.x(), center.y());
                    matrix.rotate(90);
                    QImage imgS = imgA.transformed(matrix);*/
                
                
                
                    /*QPainter painter;
                    painter.drawImage(QPoint(0,0),imgS);*/
                    /*QPrintPreviewDialog *dlg = new QPrintPreviewDialog(printer, nullptr);
                    dlg->showMaximized();*/
                    //painter.end();
                    painter.begin(printer);
                    //painter.drawImage(QPoint(0,0),imgS);
                    painter.setFont(QFont("Arial",14));
                    painter.drawText(200,200,"Hello World");
                    painter.end();
                
                }
                

                Every time US letter format vertical ... in no way A4 both vertical and horizontal ....

                bkt

                J 1 Reply Last reply 12 Apr 2019, 09:33
                0
                • G gfxx
                  12 Apr 2019, 08:56

                  @JonB said in QPrint no possible to set page size and orientation (ubuntu 18.04):

                  What do you mean "is the minor issue"? It's precisely the problem you are reporting!

                  minor because I can always transform the image and adapt it to the page ....

                  Track down just exactly what is wrong when, e.g. "impossible to set A4 format horizontal" --- is it impossible to set A4 format generally, or only if it's horizontal? Help yourself by discovering the minimal necessary/sufficient conditions.

                  Impossible in any way ... just now i try these:

                  #include "printe.h"
                  #include <QPrinter>
                  #include <QPainter>
                  #include <QPrintDialog>
                  #include <QPixmap>
                  #include <QImage>
                  #include <QTime>
                  #include <QDate>
                  #include <QDateTime>
                  #include <QDebug>
                  #include <QPdfWriter>
                  #include <QPrintPreviewDialog>
                  #include <QPrintEngine>
                  #include <QPagedPaintDevice>
                  #include <QPageLayout>
                  #include <QPageSize>
                  #include <QPaintEngine>
                  #include <QPrintPreviewWidget>
                  #include <QtPrintSupport>
                  
                  
                  
                  printe::printe()
                  {
                  
                  }
                  
                  void printe::print(QVariant data, QString imgString)
                  
                  {
                      QString baseString = "/home/Desk/Doci/folder/";
                      QString resultString = "";
                  
                      QDate cd = QDate::currentDate();
                      QTime ct = QTime::currentTime();
                  
                      if(imgString.isEmpty()){
                          QString bDate = cd.toString()+ct.toString();
                          imgString = "DATA_"+bDate;
                          resultString = baseString+imgString+".pdf";
                      }
                      else{
                          resultString = baseString+imgString+".pdf";
                      }
                  
                      qDebug() << resultString;
                  
                  
                      QPainter painter;
                      //painter.drawImage(QPoint(0,0),imgS);
                  
                      printer = new QPrinter;
                      printer->setPdfVersion(QPrinter::PdfVersion_1_6);
                      printer->setPaperSize(QSizeF(210, 297), QPrinter::Millimeter);  //(QSizeF(210, 297), QPrinter::Millimeter);
                      printer->setPageOrientation(QPageLayout::Landscape);  /* I try with Potrait too */
                      printer->setPageMargins(5, 5, 5, 5, QPrinter::Millimeter);
                      printer->setColorMode(QPrinter::Color);
                      printer->setOutputFormat(QPrinter::PdfFormat);
                      printer->printEngine();
                      printer->setResolution(600);
                      printer->setFullPage(false);
                      printer->setOutputFileName(resultString);
                  
                  
                      qDebug() << "the new Layout is:   " << printer->pageLayout();
                  
                      QImage img = qvariant_cast<QImage>(data);
                      QImage imgS = img.scaled(6912, 3408, Qt::AspectRatioMode::KeepAspectRatioByExpanding);
                      /*QPoint center = imgA.rect().center();
                      QMatrix matrix;
                      matrix.translate(center.x(), center.y());
                      matrix.rotate(90);
                      QImage imgS = imgA.transformed(matrix);*/
                  
                  
                  
                      /*QPainter painter;
                      painter.drawImage(QPoint(0,0),imgS);*/
                      /*QPrintPreviewDialog *dlg = new QPrintPreviewDialog(printer, nullptr);
                      dlg->showMaximized();*/
                      //painter.end();
                      painter.begin(printer);
                      //painter.drawImage(QPoint(0,0),imgS);
                      painter.setFont(QFont("Arial",14));
                      painter.drawText(200,200,"Hello World");
                      painter.end();
                  
                  }
                  

                  Every time US letter format vertical ... in no way A4 both vertical and horizontal ....

                  J Online
                  J Online
                  JonB
                  wrote on 12 Apr 2019, 09:33 last edited by JonB 4 Dec 2019, 09:36
                  #8

                  @gfxx
                  I suggested if you save to file that you examine the file to see what it has in it specifying layout/dimensions. I suggested some other thing to verify, like not going setFullPage(false), not including any image. It's up to you how much work you do to try to isolate the problem....

                  G 1 Reply Last reply 12 Apr 2019, 10:29
                  0
                  • J JonB
                    12 Apr 2019, 09:33

                    @gfxx
                    I suggested if you save to file that you examine the file to see what it has in it specifying layout/dimensions. I suggested some other thing to verify, like not going setFullPage(false), not including any image. It's up to you how much work you do to try to isolate the problem....

                    G Offline
                    G Offline
                    gfxx
                    wrote on 12 Apr 2019, 10:29 last edited by
                    #9

                    @JonB I try every things you suggest .... every time US vertical format .... I print only hello world ...ful page or not, color or not, with all my setting or without setting (so printer in default mode, I use only QT5.12 printer dialog) ... every time US vertical letter ......

                    regards

                    bkt

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      gfxx
                      wrote on 12 Apr 2019, 11:01 last edited by gfxx 4 Dec 2019, 11:08
                      #10

                      a completely new file ... but nothing change ........

                      testPrint.pro

                      #-------------------------------------------------
                      #
                      # Project created by QtCreator 2019-04-12T12:35:57
                      #
                      #-------------------------------------------------
                      
                      QT += core gui widgets
                      QT += printsupport
                      
                      
                      greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                      
                      TARGET = testPrinter
                      TEMPLATE = app
                      
                      # The following define makes your compiler emit warnings if you use
                      # any feature of Qt which has been marked as deprecated (the exact warnings
                      # depend on your compiler). Please consult the documentation of the
                      # deprecated API in order to know how to port your code away from it.
                      DEFINES += QT_DEPRECATED_WARNINGS
                      
                      # You can also make your code fail to compile if you use deprecated APIs.
                      # In order to do so, uncomment the following line.
                      # You can also select to disable deprecated APIs only up to a certain version of Qt.
                      #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                      
                      CONFIG += c++11
                      
                      SOURCES += \
                              main.cpp \
                              mainwindow.cpp
                      
                      HEADERS += \
                              mainwindow.h
                      
                      FORMS += \
                              mainwindow.ui
                      
                      # Default rules for deployment.
                      qnx: target.path = /tmp/$${TARGET}/bin
                      else: unix:!android: target.path = /opt/$${TARGET}/bin
                      !isEmpty(target.path): INSTALLS += target
                      
                      

                      main.ccp

                      #include "mainwindow.h"
                      #include <QApplication>
                      
                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                          MainWindow w;
                          w.show();
                      
                          return a.exec();
                      }
                      
                      

                      mainwindow.ccp

                      #include "mainwindow.h"
                      #include "ui_mainwindow.h"
                      #include <QPrinter>
                      #include <QPainter>
                      #include <QPrintDialog>
                      #include <QPixmap>
                      #include <QImage>
                      #include <QTime>
                      #include <QDate>
                      #include <QDateTime>
                      #include <QDebug>
                      #include <QPdfWriter>
                      #include <QPrintPreviewDialog>
                      #include <QPrintEngine>
                      #include <QPagedPaintDevice>
                      #include <QPageLayout>
                      #include <QPageSize>
                      #include <QPaintEngine>
                      #include <QPrintPreviewWidget>
                      #include <QtPrintSupport>
                      
                      MainWindow::MainWindow(QWidget *parent) :
                          QMainWindow(parent),
                          ui(new Ui::MainWindow)
                      {
                          ui->setupUi(this);
                      
                      
                      
                          printer = new QPrinter;
                      
                      
                          QPrintDialog *dlg = new QPrintDialog(printer, nullptr);
                      
                          dlg->setOption(QAbstractPrintDialog::PrintToFile, true);
                          dlg->addEnabledOption(QAbstractPrintDialog::PrintSelection);
                      
                          if(dlg->exec() == QDialog::Accepted) {
                              QPainter painter;
                              painter.begin(printer);
                              painter.setFont(QFont("Arial",14));
                              painter.drawText(20,20, ("some world"));
                              painter.end();
                          }
                      }
                      
                      MainWindow::~MainWindow()
                      {
                          delete ui;
                      }
                      
                      

                      so I add the image about Printer dialog after start up and after I choose Print to file:

                      0_1555066671065_printDialog1.png

                      an image of my printer choice:

                      0_1555066715727_printDialog2.png

                      and at last an image of my pint doc property:

                      0_1555066751348_printDialog3.png

                      So it seems that none of the options in the dialog box are taken into account ..... QPirint is a non free feature ??

                      Not see other ....

                      bkt

                      1 Reply Last reply
                      0
                      • fcarneyF Offline
                        fcarneyF Offline
                        fcarney
                        wrote on 19 Apr 2019, 22:43 last edited by fcarney
                        #11

                        Ran your code on:

                        Ubuntu 18.04 64 bit
                        Qt 5.12.2
                        gcc
                        

                        I get paper and orientation outputs consistent with my selections. I tried 3 variations.
                        Edit:
                        Output: print to pdf

                        C++ is a perfectly valid school of magic.

                        1 Reply Last reply
                        2

                        • Login

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