PDA

View Full Version : QPrinter - issues with page size selection (both PDF as well as real printer)



sir_clive
27th May 2015, 02:23
Hello,

I am experiencing issues with QPrinterDialog under Linux. Under Windows 7 and OS X (10.10), QPrinterDialog seems to work at least better (I still need to give it a thorough test, but at first, it looks OK)

CODE:



QPrinter printer;

printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPaperSize(QPrinter::A2);
QPrintDialog dialog(&printer, this);


PDF:

1. The preselected printer is not the PDF, but rather the real printer, as defined by CUPS. Due to the first line, I'd expect the PDF to be pre-selected.
2. If I now select the PDF printer in the dialog, and open the "Properties" window, the paper size drop-down is empty. In the "width / height" fields, however, the correct size of the A2 paper is showing, and the output will be of the correct size A2.
3. If I open the drop-down menu, the list of possible sizes show, but regardless of what I select, the (now closed) menu is empty, and the size which is showing in the dialog is A4. The output size is A4.

REAL PRINTER:

1. Upon opening the dialog, the paper size drop-down menu is empty, which is understandable, because my printer doesn't support A2. HOWEVER, the paper width / height shown in the dialog do correspond to A2
2. Opening the drop down menu shows a billion paper size options (way more than, say, under OS X for the same printer), all of them look selectable, but only some of them actually are. For example, "Photo 4x6in" is supported by my printer, but selecting it drops me back to what was previously selected, if that previous selection was correct.

Under OSX, none of the above problems seem to occur (the same code), except that I can't set the PDF as output format ("non native" or something, on OS X, printing into PDF seems to work a bit differently, I'm still an apple newbie)

I hope this explanation is making some sense! :)


Here the complete code, although only the section above is relevant:



void MainWindow::on_actionPrint_triggered()
{
QPrinter printer;

printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPaperSize(QPrinter::A2); // Default for the print dialog
QPrintDialog dialog(&printer, this);

dialog.setWindowTitle(tr("Print Document"));

if (dialog.exec() == QDialog::Accepted)
{
QPainter pp;
pp.begin(&printer);
QPagedPaintDevice::Margins margins = printer.margins();
int logicalHeight, logicalWidth;
std::cout << "paper width: " << printer.pageSizeMM().width() << " paper height: " << printer.pageSizeMM().height() << std::endl;

if (printer.orientation() == QPrinter::Portrait)
{
logicalWidth = printer.pageSizeMM().width() - (margins.left + margins.right);
logicalHeight = printer.pageSizeMM().height()- (margins.top + margins.bottom);
}
else
{
logicalWidth = printer.pageSizeMM().height() - (margins.left + margins.right);
logicalHeight = printer.pageSizeMM().width() - (margins.top + margins.bottom);
}

int physicalWidth = printer.width();
int physicalHeight = printer.height();

pp.setWindow(0, 0, logicalWidth, logicalHeight);
pp.setViewport(0, 0, physicalWidth, physicalHeight);
pp.drawRect(0, 0, logicalWidth, logicalHeight); // Drawing extents

QFont font("Courier", 3); // Seems this "3" is already in mm...
pp.setFont(font);
pp.drawText(QPointF(40, 40), "Hello, World 3!");
font.setPointSize(4); // And this "4" too! :)
pp.setFont(font);
pp.drawText(QPointF(80, 40), "Hello, World 4!");

pp.drawRect(50, 50, 100, 100);
pp.end();
}
}


Added after 6 minutes:

Sorry, forgot to mention: QT 5.4.0, Linux Ubuntu 14.04.2 LTS.

Added after 43 minutes:

Now also tried with 5.4.1 - still the same.

sir_clive
29th May 2015, 16:50
Funny that nobody experienced the same problem.

For a multi-OS environment, with some "special requirements" with respect to printing, it seems that QPrintDialog is not really useable. OS X completely ignores options (I know that's documented, but it's not a solution for me, I have to disable some options), under Ubuntu, I have the aforementioned problems. The list of supported page formats contains some 30 occurrences of code "30" ("Custom or unknown"), there is no way to set the filter which paper sizes are allowed, ... I haven't tested on Windows since the last time, so I can't say much about it.

I understand that QPrintDialog tries to use the native print dialog for printing, but that introduces some bad headaches if a program needs to run on more than just one platform.

I LOVE QT, it works nearly perfect for everything else, but printing seems not to be among its' greatest strengths. I'm afraid I'll just need to implement my own printer dialog (not derived from QAbstractPrintDialog). It's a pity!