PDA

View Full Version : QPrinter constructor ignores QPrinterInfo parameter



Al_
20th December 2014, 16:41
Hi

I try to construct a QPrinter object that is not the default printer. Thus, I first get the correct QPrinterInfo object and pass it as parameter to the constructor of QPrinter. But QPrinter still ends up with the system-wide default printer. Example code fragment:

printerInfo = QPrinterInfo::printerInfo(QStringLiteral("Farbdrucker"));
qDebug() << "printer selected in printerInfo:" << (printerInfo.isNull() ? tr("not set") : tr("%1 (%2)").arg(printerInfo.printerName()).arg(printerInfo.d escription()));
QPrinter* printer = new QPrinter(printerInfo /*, QPrinter::HighResolution*/);
qDebug() << "selected printer" << printer->printerName();

The output of the two qDebug() ... statements is as follows (unchanged, whether I include the second parameter to QPrinter or not)

printer selected in printerInfo: "Farbdrucker (Brother HL-4150CDN)"
selected printer "TLP2742"
My conclusions: the QPrinterInfo object is constructed and valid (I have named the printer 'Farbdrucker', German for color printer); QPrinterInfo finds the correct description (make and model of my color printer). But the QPrinter object nevertheless uses the default printer (TLP2742).

Where is the issue with my code / thinking?

Versions / OS:
- Ubuntu 12.04 LTS
- Qt 5.4

ChrisW67
27th December 2014, 02:07
I cannot see an obvious problem code-wise. Is the colour printer online? Is it available to the user running the code? QPrinter reports the default printer name but which printer does it actually print to?

Al_
27th December 2014, 10:19
@ChrisW67
Thanks for your hints, how to approach the problem. My extended code snipet now looks as follows:

printerInfo = QPrinterInfo::printerInfo(QStringLiteral("Farbdrucker"));
qDebug() << "printer selected in printerInfo:" << (printerInfo.isNull() ? tr("not set") : tr("%1 (%2)").arg(printerInfo.printerName()).arg(printerInfo.d escription()));
QPrinter* printer = new QPrinter(printerInfo /*, QPrinter::HighResolution*/);
qDebug() << "selected printer" << printer->printerName();
QPainter testPainter;
testPainter.begin(printer);
printer->newPage();
testPainter.end();


The color printer is connected and other applications are able to print to it.
It attempts to print to the system-wide default printer TLP2742.

When I select a different printer as system wide default, e.g., print-to-pdf, then the code snipet works properly! Thus, it seems that the issue is specifically with the printer TLP2742 (an offline printer, i.e., it should print to a file). I will further look into this and report back. (I still consider it unexpected behavior / bug that a potentially misconfigured printer TLP2742 messes up the printer selection in a Qt program that deliberately avoids to print to the TLP2742).

[EDIT]
I can now confirm that the issue is related to the driver for the default printer that is incomplete or broken. Still not good, of course: this means that an end-user error outside of my control has the potential to break some functionality of my Qt application.