QList<QPrinterInfo> QPrinterInfo::availablePrinters() { QList<QPrinterInfo> printers; DWORD needed = 0; DWORD returned = 0; if (!EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 4, 0, 0, &needed, &returned)) { LPBYTE buffer = new BYTE[needed]; if (EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 4, buffer, needed, &needed, &returned)) { PPRINTER_INFO_4 infoList = reinterpret_cast<PPRINTER_INFO_4>(buffer); QPrinterInfo defPrn = defaultPrinter(); for (uint i = 0; i < returned; ++i) { QString printerName(QString::fromWCharArray(infoList[i].pPrinterName)); QPrinterInfo printerInfo(printerName); if (printerInfo.printerName() == defPrn.printerName()) printerInfo.d_ptr->isDefault = true; printers.append(printerInfo); } } delete [] buffer; } return printers; }
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // HelloWorld helloWorld; // HelloWorld helloWorld2; // HelloWorld helloWorld3; // // QThread t; // QThread t2; // QThread t3; // // helloWorld.moveToThread(&t); // helloWorld2.moveToThread(&t2); // helloWorld3.moveToThread(&t3); //// // a.connect(&t2, SIGNAL(started()), &helloWorld2, SLOT(sayGoodBye())); // a.connect(&t, SIGNAL(started()), &helloWorld, SLOT(sayHello())); // a.connect(&t3, SIGNAL(started()), &helloWorld3, SLOT(sayHello())); // a.connect(&helloWorld, SIGNAL( done() ), &a, SLOT( quit() ), Qt::QueuedConnection); // // t.start(); // t2.start(); // t3.start(); Foo f1; Foo f2; f1.AddItem(0,"Hiya!"); printf("\nSize: %4i",f1.getLength()); f1.AddItem(1,"Hiya!"); printf("\nSize: %4i",f1.getLength()); f2.AddItem(2,"Heya"); printf("\nSize: %4i",f2.getLength()); f2.AddItem(3,"Heya"); printf("\nSize: %4i",f2.getLength()); printf("\nSize: %4i",f1.getLength()); printf("\n%s",Foo::doWonky(0).toAscii().data()); printf("\n%s",Foo::doWonky(1).toAscii().data()); printf("\n%s",Foo::doWonky(2).toAscii().data()); printf("\n%s",Foo::doWonky(3).toAscii().data()); fprintf(stdout, "\nStarting up... 0x%08x\n",&a); QPrinterInfo pInfo = QPrinterInfo::defaultPrinter(); fprintf(stdout, "\n %s\n",pInfo.printerName().toAscii().data()); return a.exec(); }
QString RSettings::getDefaultPrinterName() { #if QT_VERSION >= 0x050300 return QPrinterInfo::defaultPrinterName(); #else QPrinterInfo printer = QPrinterInfo::defaultPrinter(); if (printer.isNull()) { return QString(); } return printer.printerName(); #endif }
static PyObject *meth_QPrinterInfo_printerName(PyObject *sipSelf, PyObject *sipArgs) { PyObject *sipParseErr = NULL; { QPrinterInfo *sipCpp; if (sipParseArgs(&sipParseErr, sipArgs, "B", &sipSelf, sipType_QPrinterInfo, &sipCpp)) { QString *sipRes; Py_BEGIN_ALLOW_THREADS sipRes = new QString(sipCpp->printerName()); Py_END_ALLOW_THREADS return sipConvertFromNewType(sipRes,sipType_QString,NULL); } }
void QCupsPrintEnginePrivate::closePrintDevice() { QPdfPrintEnginePrivate::closePrintDevice(); if (!cupsTempFile.isEmpty()) { QString tempFile = cupsTempFile; cupsTempFile.clear(); QCUPSSupport cups; // Set up print options. QByteArray prnName; QList<QPair<QByteArray, QByteArray> > options; QVector<cups_option_t> cupsOptStruct; if (!printerName.isEmpty()) { prnName = printerName.toLocal8Bit(); } else { QPrinterInfo def = QPrinterInfo::defaultPrinter(); if (def.isNull()) { qWarning("Could not determine printer to print to"); QFile::remove(tempFile); return; } prnName = def.printerName().toLocal8Bit(); } if (!cupsStringPageSize.isEmpty()) options.append(QPair<QByteArray, QByteArray>("media", cupsStringPageSize.toLocal8Bit())); if (copies > 1) options.append(QPair<QByteArray, QByteArray>("copies", QString::number(copies).toLocal8Bit())); if (collate) options.append(QPair<QByteArray, QByteArray>("Collate", "True")); switch (duplex) { case QPrinter::DuplexNone: options.append(QPair<QByteArray, QByteArray>("sides", "one-sided")); break; case QPrinter::DuplexAuto: if (!landscape) options.append(QPair<QByteArray, QByteArray>("sides", "two-sided-long-edge")); else options.append(QPair<QByteArray, QByteArray>("sides", "two-sided-short-edge")); break; case QPrinter::DuplexLongSide: options.append(QPair<QByteArray, QByteArray>("sides", "two-sided-long-edge")); break; case QPrinter::DuplexShortSide: options.append(QPair<QByteArray, QByteArray>("sides", "two-sided-short-edge")); break; } if (QCUPSSupport::cupsVersion() >= 10300 && landscape) options.append(QPair<QByteArray, QByteArray>("landscape", "")); QStringList::const_iterator it = cupsOptions.constBegin(); while (it != cupsOptions.constEnd()) { options.append(QPair<QByteArray, QByteArray>((*it).toLocal8Bit(), (*(it+1)).toLocal8Bit())); it += 2; } for (int c = 0; c < options.size(); ++c) { cups_option_t opt; opt.name = options[c].first.data(); opt.value = options[c].second.data(); cupsOptStruct.append(opt); } // Print the file. cups_option_t* optPtr = cupsOptStruct.size() ? &cupsOptStruct.first() : 0; cups.printFile(prnName.constData(), tempFile.toLocal8Bit().constData(), title.toLocal8Bit().constData(), cupsOptStruct.size(), optPtr); QFile::remove(tempFile); } }
//debug: to get the html content qDebug(m_html.toUtf8()); //debug: to get available printer in local area QString m_printers = Printer::printerList(); qDebug(m_printers.toUtf8()); //find the wanted network printer to print QPrinterInfo printerInfo = QPrinterInfo(); QPrinterInfo targetPrinterInfo = QPrinterInfo(); foreach(QPrinterInfo item, printerInfo.availablePrinters()){ if(item.printerName() == QString::fromStdString("HP_LaserJet_P4014.P4015_PCL6:3")){ //m_printer = new QPrinter(item, QPrinter::HighResolution); targetPrinterInfo = item; qDebug(targetPrinterInfo.printerName().toUtf8()); break; }else{ qDebug("nothing!!!"); } } //QPrinter m_printer(targetPrinterInfo, QPrinter::HighResolution); QPrinter m_printer(QPrinter::HighResolution); //m_printer.setOutputFormat(QPrinter::NativeFormat); m_printer.setOutputFileName("/home/ljl/menu.pdf"); QTextDocument m_textDocument; m_textDocument.setHtml(m_html); //QTextDocument::setPlainText(const QString &text) m_textDocument.setPageSize(QSizeF(m_printer.logicalDpiX()*(75/50),
/* static Receipts::ReceiptsPlugin *receiptsPlugin() { qff::MainWindow *fwk = qff::MainWindow::frameWork(); auto *ret = qobject_cast<Receipts::ReceiptsPlugin *>(fwk->plugin("Receipts")); QF_ASSERT(ret != nullptr, "Bad plugin", return 0); return ret; } */ void ReceiptsPrinter::printReceipt(const QString &report_file_name, const QVariantMap &report_data) { qfLogFuncFrame(); QF_TIME_SCOPE("ReceiptsPrinter::printReceipt()"); const ReceiptsPrinterOptions &printer_opts = m_printerOptions; QPrinter *printer = nullptr; QPaintDevice *paint_device = nullptr; if(printer_opts.printerType() == (int)ReceiptsPrinterOptions::PrinterType::GraphicPrinter) { QF_TIME_SCOPE("init graphics printer"); QPrinterInfo pi = QPrinterInfo::printerInfo(printer_opts.graphicsPrinterName()); if(pi.isNull()) { for(auto s : QPrinterInfo::availablePrinterNames()) { qfInfo() << "available printer:" << s; } pi = QPrinterInfo::defaultPrinter(); } if(pi.isNull()) { qfWarning() << "Default printer not set"; return; } qfInfo() << "printing on:" << pi.printerName(); printer = new QPrinter(pi); paint_device = printer; } else { qfInfo() << "printing on:" << printer_opts.characterPrinterModel() << "at:" << printer_opts.characterPrinterDevice(); qff::MainWindow *fwk = qff::MainWindow::frameWork(); paint_device = fwk; } qf::qmlwidgets::reports::ReportProcessor rp(paint_device); { QF_TIME_SCOPE("setting report and data"); rp.setReport(report_file_name); for(auto key : report_data.keys()) { rp.setTableData(key, report_data.value(key)); } } if(printer_opts.printerType() == (int)ReceiptsPrinterOptions::PrinterType::GraphicPrinter) { QF_TIME_SCOPE("process graphics"); { QF_TIME_SCOPE("process report"); rp.process(); } qf::qmlwidgets::reports::ReportItemMetaPaintReport *doc; { QF_TIME_SCOPE("getting processor output"); doc = rp.processorOutput(); } qf::qmlwidgets::reports::ReportItemMetaPaint *it = doc->child(0); if(it) { QF_TIME_SCOPE("draw meta-paint"); qf::qmlwidgets::reports::ReportPainter painter(paint_device); painter.drawMetaPaint(it); } QF_SAFE_DELETE(printer); } else if(printer_opts.printerType() == (int)ReceiptsPrinterOptions::PrinterType::CharacterPrinter) { QDomDocument doc; doc.setContent(QLatin1String("<?xml version=\"1.0\"?><report><body/></report>")); QDomElement el_body = doc.documentElement().firstChildElement("body"); qf::qmlwidgets::reports::ReportProcessor::HtmlExportOptions opts; opts.setConvertBandsToTables(false); rp.processHtml(el_body, opts); //qfInfo() << doc.toString(); QList<QByteArray> data_lines = createPrinterData(el_body, printer_opts); auto save_file = [data_lines](const QString &fn) { QFile f(fn); if(f.open(QFile::WriteOnly)) { for(QByteArray ba : data_lines) { f.write(ba); f.write("\n"); } } else { qfError() << "Cannot open file" << f.fileName() << "for writing!"; } }; if(!printer_opts.characterPrinterDirectory().isEmpty()) { QString fn = printer_opts.characterPrinterDirectory(); qf::core::utils::FileUtils::ensurePath(fn); QCryptographicHash ch(QCryptographicHash::Sha1); for(QByteArray ba : data_lines) ch.addData(ba); fn += '/' + QString::fromLatin1(ch.result().toHex().mid(0, 8)) + ".txt"; save_file(fn); } else if (!printer_opts.characterPrinterDevice().isEmpty()) { save_file(printer_opts.characterPrinterDevice()); } } }