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. QPrintDialog can not set custom paper size

QPrintDialog can not set custom paper size

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.2k 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.
  • hooanghiepH Offline
    hooanghiepH Offline
    hooanghiep
    wrote on last edited by hooanghiep
    #1

    i am using QPrintDialog for specifying the printer's configuration.
    This is how i tried:

    QTextDocument *document = new QTextDocument();
    
    QPrinter printer;
    
    printer.setPageSize(QPrinter::A4);//default
    printer.setOutputFormat(QPrinter::PdfFormat);
    
    document->setPageSize(printer.pageRect().size()); 
    document->setHtml(report::mReportContent);
    QPrintDialog *dlg = new QPrintDialog(&printer,0);
    
    dlg->setOption( QAbstractPrintDialog::PrintToFile,        false );
    dlg->setOption( QAbstractPrintDialog::PrintSelection,     true );
    dlg->setOption( QAbstractPrintDialog::PrintPageRange,     false );
    dlg->setOption( QAbstractPrintDialog::PrintShowPageSize,  true );
    dlg->setOption( QAbstractPrintDialog::PrintCollateCopies, false );
    dlg->setOption( QAbstractPrintDialog::PrintCurrentPage, false );
    if(dlg->exec() == QDialog::Accepted)
    {
        //pre set paper size to A5
        document->print(&printer);
    }
    delete dlg;
    delete document;
    

    ======================================
    By default i set paper size to A4, everything is ok
    But when dialog open and i set any different paper size(like A5) then it is back to A4.
    My question is that how can i set custom paper size to the printer?

    p/s My OS is ubuntu 16.04

    Thanks,
    Hiep

    JonBJ Pl45m4P 2 Replies Last reply
    0
    • hooanghiepH hooanghiep

      i am using QPrintDialog for specifying the printer's configuration.
      This is how i tried:

      QTextDocument *document = new QTextDocument();
      
      QPrinter printer;
      
      printer.setPageSize(QPrinter::A4);//default
      printer.setOutputFormat(QPrinter::PdfFormat);
      
      document->setPageSize(printer.pageRect().size()); 
      document->setHtml(report::mReportContent);
      QPrintDialog *dlg = new QPrintDialog(&printer,0);
      
      dlg->setOption( QAbstractPrintDialog::PrintToFile,        false );
      dlg->setOption( QAbstractPrintDialog::PrintSelection,     true );
      dlg->setOption( QAbstractPrintDialog::PrintPageRange,     false );
      dlg->setOption( QAbstractPrintDialog::PrintShowPageSize,  true );
      dlg->setOption( QAbstractPrintDialog::PrintCollateCopies, false );
      dlg->setOption( QAbstractPrintDialog::PrintCurrentPage, false );
      if(dlg->exec() == QDialog::Accepted)
      {
          //pre set paper size to A5
          document->print(&printer);
      }
      delete dlg;
      delete document;
      

      ======================================
      By default i set paper size to A4, everything is ok
      But when dialog open and i set any different paper size(like A5) then it is back to A4.
      My question is that how can i set custom paper size to the printer?

      p/s My OS is ubuntu 16.04

      Thanks,
      Hiep

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @hooanghiep
      It may not help you, but under Ubuntu/Mint/etc. I believe yours is same as reported recently in https://forum.qt.io/topic/100529/qprintdialog ?

      hooanghiepH 1 Reply Last reply
      3
      • JonBJ JonB

        @hooanghiep
        It may not help you, but under Ubuntu/Mint/etc. I believe yours is same as reported recently in https://forum.qt.io/topic/100529/qprintdialog ?

        hooanghiepH Offline
        hooanghiepH Offline
        hooanghiep
        wrote on last edited by hooanghiep
        #3

        @JonB thanks for your inform :)

        1 Reply Last reply
        0
        • hooanghiepH hooanghiep

          i am using QPrintDialog for specifying the printer's configuration.
          This is how i tried:

          QTextDocument *document = new QTextDocument();
          
          QPrinter printer;
          
          printer.setPageSize(QPrinter::A4);//default
          printer.setOutputFormat(QPrinter::PdfFormat);
          
          document->setPageSize(printer.pageRect().size()); 
          document->setHtml(report::mReportContent);
          QPrintDialog *dlg = new QPrintDialog(&printer,0);
          
          dlg->setOption( QAbstractPrintDialog::PrintToFile,        false );
          dlg->setOption( QAbstractPrintDialog::PrintSelection,     true );
          dlg->setOption( QAbstractPrintDialog::PrintPageRange,     false );
          dlg->setOption( QAbstractPrintDialog::PrintShowPageSize,  true );
          dlg->setOption( QAbstractPrintDialog::PrintCollateCopies, false );
          dlg->setOption( QAbstractPrintDialog::PrintCurrentPage, false );
          if(dlg->exec() == QDialog::Accepted)
          {
              //pre set paper size to A5
              document->print(&printer);
          }
          delete dlg;
          delete document;
          

          ======================================
          By default i set paper size to A4, everything is ok
          But when dialog open and i set any different paper size(like A5) then it is back to A4.
          My question is that how can i set custom paper size to the printer?

          p/s My OS is ubuntu 16.04

          Thanks,
          Hiep

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @hooanghiep

          Do you really need a custom size or is your paper just not standard A4 / A5 / A3?
          Here are all default paper size options.

          Is

          printer.pageRect().size()
          

          a valid format?

          What happens, if you comment this "printer.setPageSize(QPrinter::A4);//default" line? I think your selection gets overwritten by your default value (A4).
          Have you tried to debug output the actual paper size while the dialog is running?

          From https://doc.qt.io/qt-5/qpagedpaintdevice.html#setPageSize:

          To get the current QPageSize use pageLayout().pageSize().
          
          You should call this before calling QPainter::begin(), or immediately before calling newPage() to apply the new page size to a new page. You should not call any painting methods between a call to setPageSize() and newPage() as the wrong paint metrics may be used.
          
          Returns true if the page size was successfully set to pageSize.
          

          EDIT: Maybe you have to set the PageSize to EnumVal "30" (= ::Custom) first, to make the printer accept your own values?


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          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