Stanimirovv
Stanimirovv

Reputation: 3172

Qt create pdf example - can't get it to work

I looked at some examples and decided to implement one of them. It compiles and doesn't crash when ran, however It doesn't create the pdf, it throws some error (which I do not understand). The question is where there error is and how can it be removed?

The project code:

#-------------------------------------------------
#
# Project created by QtCreator 2013-06-08T10:07:11
#
#-------------------------------------------------

QT       += core
QT       -= gui
QT += printsupport
TARGET = PDFPrintMaybe
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp

And the source itself:

#include <QTextDocument>
#include <QPrinter>
#include <QApplication>

int main( int argc, char **argv )
{
    QApplication app( argc, argv );

  QTextDocument doc;
  doc.setHtml( "<p>A QTextDocument can be used to present formatted text "
               "in a nice way.</p>"
               "<p align=center>It can be <b>formatted</b> "
               "<font size=+2>in</font> <i>different</i> ways.</p>"
               "<p>The text can be really long and contain many "
               "paragraphs. It is properly wrapped and such...</p>" );
   QPrinter printer;
   printer.setOutputFileName("C:\\Users\\SameTime\\Desktop");
   printer.setOutputFormat(QPrinter::PdfFormat);
   doc.print(&printer);
   printer.newPage();

  return 0;
}

And finally, the error itself:

QPainter:: begin(): Returned false

Upvotes: 1

Views: 6604

Answers (1)

Lol4t0
Lol4t0

Reputation: 12547

"C:\\Users\\SameTime\\Desktop"

probably refers to the existing folder, not a file name.

You should specify your pdf file name instead, like

"C:\\Users\\SameTime\\Desktop\\1.pdf"

And make sure that path to the file exists and accessible.

Otherwise, sustem would not be able to crate pdf and print (i.e. paint on the pdf canvas)

Upvotes: 1

Related Questions