Antonio L.
29th June 2016, 09:01
This application generates a test pdf file using QPrinter and QPainter. The call to QPainter::begin() opens a dialog used to enter the PDF file name and location:
#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;
}
It works correctly when a PDF file name is introduced in the dialog. However, by clicking on the Cancel button the application crashes during the call to QPainter::begin().
Is there a way to prevent this crash? Is the previous code correct, or is there something missing?
Thanks in advance and best regards.
#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;
}
It works correctly when a PDF file name is introduced in the dialog. However, by clicking on the Cancel button the application crashes during the call to QPainter::begin().
Is there a way to prevent this crash? Is the previous code correct, or is there something missing?
Thanks in advance and best regards.