QPrintDialog can not set custom paper size



  • 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



  • @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 ?



  • @JonB thanks for your inform :)



  • @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?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.