作者:佳佳的梦Aas | 来源:互联网 | 2023-10-11 10:25
Qt调用打印机批量打印图片,输出为pdf;voidshowPrintDialog(constQStringList&paths){QPrinterprinter;p
Qt 调用打印机批量打印图片,输出为pdf;
void showPrintDialog(const QStringList &paths) {QPrinter printer;printer.setOutputFormat(QPrinter::PdfFormat);QPixmap img;QPrintDialog* printDialog &#61; new QPrintDialog(&printer, this);printDialog->resize(400, 300);m_printDialogVisible &#61; true;if (printDialog->exec() &#61;&#61; QDialog::Accepted) {QPainter painter(&printer);QList::const_iterator i;for(i &#61; paths.begin(); i!&#61; paths.end(); &#43;&#43;i){if (!img.load(*i)) {qDebug() <<"img load failed" <<*i;continue;}if (img.width() > img.height())printer.setPageOrientation(QPageLayout::Landscape);elseprinter.setPageOrientation(QPageLayout::Portrait);QRect pageOriginRect &#61; printer.pageRect();QSize pageRect &#61; QSize(pageOriginRect.width() - 8,pageOriginRect.height() - 8);img &#61; img.scaled(pageRect, Qt::KeepAspectRatio, Qt::SmoothTransformation);painter.drawPixmap(0, 0, img);if (i !&#61; paths.end() - 1)printer.newPage();}painter.end();qDebug() <<"print succeed!";return;}QObject::connect(printDialog, &QPrintDialog::finished, this, [&#61;]{printDialog->deleteLater();m_printDialogVisible &#61; false;});qDebug() <<"print failed!";
}