PDA

View Full Version : How to set default printer in QPrintDialog?



Al_
30th December 2014, 14:20
My application commonly uses a specific printer (that prints on labels); this is not the system wide default printer. Thus, the QPrintDialog should open with the label-printer pre-selected. While I have a solution (shown below), it is a very ugly work-around (tested on Ubuntu 14.04 / Qt 5.4) that I expect to fail on other OS or in future Qt versions.

QPrinter* printer = printerInfo.isNull() ? new QPrinter(QPrinter::HighResolution) : new QPrinter(printerInfo, QPrinter::HighResolution);
qDebug() << "next show print dialog for printer" << printer->printerName();
QPrintDialog printDlg(printer);
qDebug() << "selected in print dialog" << printer->printerName() << ", but should be" << printerInfo.printerName();
QList<QWidget*> childWidgets = printDlg.findChildren<QWidget*>(QLatin1String("printers"), Qt::FindChildrenRecursively);
if (childWidgets.count() == 1) {
QComboBox* comboBox(qobject_cast<QComboBox*>(childWidgets.at(0)));
if (comboBox && comboBox->findText(printerInfo.printerName())) {
comboBox->setCurrentText(printerInfo.printerName());}}
if (printDlg.exec() == QDialog::Accepted) {
The output from lines 2 and 4 shows that providing a pre-selected QPrinter object to the constructor of QPrintDialog does not do the trick:

next show print dialog for printer "psc-1100-series"
selected in print dialog "Farbdrucker" , but should be "psc-1100-series"
Is there a clean way to have an application-specific printer pre-selected in a QPrintDialog?