#include <iostream>
#include <QApplication>
#include <QPrinterInfo>
#include <QPainter>
int main(int argc, char *argv[])
{
foreach(QPrinterInfo printerInfo, QPrinterInfo::availablePrinters()) {
if (printerInfo.
state() == QPrinter::PrinterState::Error) continue;
// Look for the virtual printer device that generates a pdf.
if (printerInfo.printerName() == "Microsoft Print to PDF")
{
// This statement pops up a file selection dialog.
// When it is cancelled, the application crashes ...
qPainter->begin(qPrinter);
// ... and this statement is never reached.
std::cout << "Starting printing on the pdf file." << std::endl;
// We print some text in the PDF file.
qPainter->drawText(100, 100, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
qPrinter->newPage();
qPainter->drawText(100, 100, "Mauris ut urna eget dui eleifend placerat.");
qPrinter->newPage();
// Close the printer and clean-up.
qPainter->end();
delete qPrinter;
delete qPainter;
}
}
return 0;
}
#include <iostream>
#include <QApplication>
#include <QPrinterInfo>
#include <QPainter>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
foreach(QPrinterInfo printerInfo, QPrinterInfo::availablePrinters()) {
if (printerInfo.state() == QPrinter::PrinterState::Error)
continue;
// Look for the virtual printer device that generates a pdf.
if (printerInfo.printerName() == "Microsoft Print to PDF")
{
QPrinter * qPrinter = new QPrinter(printerInfo, QPrinter::HighResolution);
QPainter * qPainter = new QPainter();
// This statement pops up a file selection dialog.
// When it is cancelled, the application crashes ...
qPainter->begin(qPrinter);
// ... and this statement is never reached.
std::cout << "Starting printing on the pdf file." << std::endl;
// We print some text in the PDF file.
qPainter->drawText(100, 100, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
qPrinter->newPage();
qPainter->drawText(100, 100, "Mauris ut urna eget dui eleifend placerat.");
qPrinter->newPage();
// Close the printer and clean-up.
qPainter->end();
delete qPrinter;
delete qPainter;
}
}
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks