//synchronous method #include <QtGui> int main(int argc, char **argv) { QApplication app(argc, argv); QPrinter printer; printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName("/foobar/nonwritable.pdf"); QPainter painter; if (! painter.begin(&printer)) { // failed to open file qWarning("failed to open file, is it writable?"); return 1; } painter.drawText(10, 10, "Test"); if (! printer.newPage()) { qWarning("failed in flushing page to disk, disk full?"); return 1; } painter.drawText(10, 10, "Test 2"); painter.end(); return 0; } //asynchronous method #include <QObject> class Object : public QObject { Q_OBJECT public: Object(QObject *parent = 0); public slots: void print(); }; #include <QtGui> #include "object.h" Object::Object(QObject *parent) : QObject(parent) { } void Object::print() { int numberOfPages = 10; int lastPage = numberOfPages - 1; QPrinter printer(QPrinter::HighResolution); printer.setOutputFileName("print.ps"); QPainter painter; painter.begin(&printer); for (int page = 0; page < numberOfPages; ++page) { // Use the painter to draw on the page. if (page != lastPage) printer.newPage(); } painter.end(); qApp->quit(); } #include <QtGui> #include "object.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Object object; QTimer timer; timer.setSingleShot(true); timer.connect(&timer, SIGNAL(timeout()), &object, SLOT(print())); timer.start(0); return app.exec(); }