PDA

View Full Version : QPrinter without QPrintDialog



jefftee
4th July 2016, 08:24
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:



QPrinter printer_(QPrinter::HighResolution);
printer_.setPrinterName("Canon_PRO_100_series");
printer_.setResolution(300);
printer_.setPageLayout(page_layout);
printer_.setCopyCount(1);
printer_.setDoubleSidedPrinting(false);
printer_.setDuplex(QPrinter::DuplexNone);
printer_.setColorMode(QPrinter::Color);
printer_.setPageSize(QPrinter::Letter);
printer_.setPaperSize(QPrinter::Letter);
printer_.setPaperSource(QPrinter::Auto);
printer_.setCreator("Inkjet Plumber");
printer_.setOrientation(QPrinter::Portrait);
printer_.setPageMargins(.5, .5, .5, .5, QPrinter::Inch);
printer_.setPageLayout(page_layout);
printer_.setDocName("Inkjet Plumber Maintenance Job");
// printer_.setOutputFileName("/Users/jefft/Downloads/ijp.pdf");
// printer_.setOutputFormat(QPrinter::PdfFormat);
printer_.setOutputFormat(QPrinter::NativeFormat);

if (!printer_.isValid())
qDebug("Printer is invalid!");

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:



void MainWindow::paint_page(QPrinter *printer)
{
QPainter painter;

painter.begin(printer);

painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::black);
// Draw page rect to show printable page (excluding margins)
// painter.drawRect(painter_rect);
painter.setFont(QFont("Tahoma", 10));
painter.drawText(0, 0, "Inkjet Plumber maintenance job sent to " + printer_info_.printerName() + ": " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss."));
painter.setPen(QPen(Qt::black));
print_swatch(painter, 0, 50, Qt::cyan);
print_swatch(painter, 200, 50, Qt::magenta);
print_swatch(painter, 400, 50, Qt::yellow);
print_swatch(painter, 600, 50, Qt::black);
print_swatch(painter, 800, 50, Qt::gray);
print_swatch(painter, 1000, 50, Qt::lightGray);
print_swatch(painter, 1200, 50, Qt::red);
print_swatch(painter, 1400, 50, Qt::green);
print_swatch(painter, 1600, 50, Qt::blue);

painter.end();

return;
}

Here's the print preview code that works fine and prints fine if I print using the QPrintPreviewDialog:



QPrintPreviewDialog preview_dlg(&printer_);
connect(&preview_dlg, &QPrintPreviewDialog::paintRequested, this, &MainWindow::paint_page);
int result = preview_dlg.exec();

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:



QPrintDialog print_dlg(&printer_);
result = print_dlg.exec();
if (result == true)
paint_page(&printer_);

Needless to say, this is driving me bonkers! Can anyone see why I might be having an issue printing without using QPrintDialog?

Thanks,

jefftee

anda_skoa
4th July 2016, 09:47
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,
_

jefftee
4th July 2016, 11:01
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:



QPrintDialog print_dlg(&printer_);
print_dlg.accept();

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.