Results 1 to 3 of 3

Thread: QPrinter without QPrintDialog

  1. #1
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default QPrinter without QPrintDialog

    Hi, I'm trying to print directly to a QPrinter that I constructed without using QPrintDialog. If I use QPrintDialog, the page prints as expected, however, when I don't use QPrintDialog, the printed page on my Canon PIXMA Pro-100 inkjet printer comes out extremely small. The same code works OK if I print to a different printer, so the problem seems to be specific to my Canon inkjet printer!

    Here's the QPrinter initialization code that I'm using:

    Qt Code:
    1. QPrinter printer_(QPrinter::HighResolution);
    2. printer_.setPrinterName("Canon_PRO_100_series");
    3. printer_.setResolution(300);
    4. printer_.setPageLayout(page_layout);
    5. printer_.setCopyCount(1);
    6. printer_.setDoubleSidedPrinting(false);
    7. printer_.setDuplex(QPrinter::DuplexNone);
    8. printer_.setColorMode(QPrinter::Color);
    9. printer_.setPageSize(QPrinter::Letter);
    10. printer_.setPaperSize(QPrinter::Letter);
    11. printer_.setPaperSource(QPrinter::Auto);
    12. printer_.setCreator("Inkjet Plumber");
    13. printer_.setOrientation(QPrinter::Portrait);
    14. printer_.setPageMargins(.5, .5, .5, .5, QPrinter::Inch);
    15. printer_.setPageLayout(page_layout);
    16. printer_.setDocName("Inkjet Plumber Maintenance Job");
    17. // printer_.setOutputFileName("/Users/jefft/Downloads/ijp.pdf");
    18. // printer_.setOutputFormat(QPrinter::PdfFormat);
    19. printer_.setOutputFormat(QPrinter::NativeFormat);
    20.  
    21. if (!printer_.isValid())
    22. qDebug("Printer is invalid!");
    To copy to clipboard, switch view to plain text mode 
    If I uncomment lines 16-17 and print to PDF, the PDF file looks as expected and prints fine using the Mac Preview app. The code I use to render the page is:

    Qt Code:
    1. void MainWindow::paint_page(QPrinter *printer)
    2. {
    3. QPainter painter;
    4.  
    5. painter.begin(printer);
    6.  
    7. painter.setRenderHint(QPainter::Antialiasing);
    8. painter.setPen(Qt::black);
    9. // Draw page rect to show printable page (excluding margins)
    10. // painter.drawRect(painter_rect);
    11. painter.setFont(QFont("Tahoma", 10));
    12. painter.drawText(0, 0, "Inkjet Plumber maintenance job sent to " + printer_info_.printerName() + ": " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss."));
    13. painter.setPen(QPen(Qt::black));
    14. print_swatch(painter, 0, 50, Qt::cyan);
    15. print_swatch(painter, 200, 50, Qt::magenta);
    16. print_swatch(painter, 400, 50, Qt::yellow);
    17. print_swatch(painter, 600, 50, Qt::black);
    18. print_swatch(painter, 800, 50, Qt::gray);
    19. print_swatch(painter, 1000, 50, Qt::lightGray);
    20. print_swatch(painter, 1200, 50, Qt::red);
    21. print_swatch(painter, 1400, 50, Qt::green);
    22. print_swatch(painter, 1600, 50, Qt::blue);
    23.  
    24. painter.end();
    25.  
    26. return;
    27. }
    To copy to clipboard, switch view to plain text mode 
    Here's the print preview code that works fine and prints fine if I print using the QPrintPreviewDialog:

    Qt Code:
    1. QPrintPreviewDialog preview_dlg(&printer_);
    2. connect(&preview_dlg, &QPrintPreviewDialog::paintRequested, this, &MainWindow::paint_page);
    3. int result = preview_dlg.exec();
    To copy to clipboard, switch view to plain text mode 
    and finally the QPrintDialog snippet that works fine as well, but this program is a utility that I need to print w/o user intervention, so the QPrintDialog isn't really an option:

    Qt Code:
    1. QPrintDialog print_dlg(&printer_);
    2. result = print_dlg.exec();
    3. if (result == true)
    4. paint_page(&printer_);
    To copy to clipboard, switch view to plain text mode 
    Needless to say, this is driving me bonkers! Can anyone see why I might be having an issue printing without using QPrintDialog?

    Thanks,

    jefftee
    Last edited by jefftee; 4th July 2016 at 08:47.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPrinter without QPrintDialog

    One guess would be that the print dialog interacts with the printer driver in some way and gets back some data that it attaches to the printer which is then later on passed back to the driver.

    Cheers,
    _

  3. #3
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QPrinter without QPrintDialog

    Thanks @anda_skoa, it's odd that it's just the one printer that exhibits this behavior, works fine with other printers I've tested. I did find a workaround, which is sort of a kludge. If I instantiate a QPrintDialog and accept it, without showing or exec'ing it, it seems to work, so the following code addition now prints w/o user intervention:

    Qt Code:
    1. QPrintDialog print_dlg(&printer_);
    2. print_dlg.accept();
    To copy to clipboard, switch view to plain text mode 
    The doc seems to indicate that you should be able to construct a QPrinter w/o using QPrintDialog and it does work for all but one of my printers, so perhaps there's something unique about the Canon print drivers under OSX, etc.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. QPrinter and QPrintDialog
    By Alexander111122 in forum Newbie
    Replies: 0
    Last Post: 29th March 2016, 11:16
  2. QPrinter and QPrintDialog
    By Alexander111122 in forum Newbie
    Replies: 2
    Last Post: 25th March 2016, 11:59
  3. QPrinter / QPrintDialog issue
    By d_stranz in forum Qt Programming
    Replies: 1
    Last Post: 4th June 2014, 22:55
  4. Replies: 1
    Last Post: 14th May 2013, 07:54
  5. How to use QPrinter without QPrintDialog?
    By pascal456 in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2006, 19:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.