我查看了一些例子并决定实施其中一个例子。它运行时编译并且不会崩溃,但它不会创建pdf,它会抛出一些错误(我不明白)。问题是错误在哪里以及如何删除?
项目代码:
#-------------------------------------------------
#
# 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
源头本身:
#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;
}
最后,错误本身:
QPainter:: begin(): Returned false
答案 0 :(得分:1)
"C:\\Users\\SameTime\\Desktop"
可能是指现有文件夹,而不是文件名。
您应该指定您的pdf文件名,例如
"C:\\Users\\SameTime\\Desktop\\1.pdf"
确保该文件的路径存在且可访问。
否则,sustem将无法创建pdf并打印(即在pdf画布上 paint )