Solved Printing without QPrintDialog (not on default printer)
-
Hi,
there is a way to print a page on a specific printer without the user interaction?I have 2 printers:
Canon XXXX
DYMO LabelWriter 450 <-- this is the default printerNow I'd like to print on a Canon XXXX directly by code and without to show the QPrintDialog.
How can I do that?I can iterate on QPrinterInfo list... but it doesn't return a QPrinter object.
QList<QPrinterInfo> plist = QPrinterInfo::availablePrinters(); qDebug() << "numbers of printers:" << plist.length(); foreach (QPrinterInfo pinfo, plist) { qDebug() << "printer name:" << pinfo.printerName(); }
-
@addebito
https://doc.qt.io/qt-5/qprinter.html#QPrinter-1 constructor takes aQPrinterInfo
, which is what you have in yourQPrinterInfo::availablePrinters()
list. -
Oh my god, it was so easy...
QString printerName = "Canon XXXX"; QList<QPrinterInfo> plist = QPrinterInfo::availablePrinters(); foreach (QPrinterInfo pinfo, plist) { qDebug() << "printer name:" << pinfo.printerName(); if (printerName.compare(pinfo.printerName()) == 0) { QPrinter printer(pinfo); m_report->printReport(&printer); break; } }
Thank you so much @JonB
-
@addebito
Even simpler: you don't need to do the search yourself :) Just usestatic QPrinterInfo QPrinterInfo::printerInfo(const QString &printerName)
, https://doc.qt.io/qt-5/qprinterinfo.html#printerInfo ! -
QString printerName = "Canon XXXX"; QPrinterInfo pinfo = QPrinterInfo::printerInfo(printerName); if (!pinfo.isNull()) { QPrinter printer(pinfo); m_report->printReport(&printer); } else { // show user message: printer not found... }
Thanks again @JonB