我使用了我在 Qt pdf 打印上能找到的最简单的代码,它运行良好,没有任何错误。但是,当我尝试打开生成的 pdf 时,它抱怨 pdf 为空且无法打开。我不知道代码的哪个方面是错误的或可能已过时?我虽然可能是权限问题,但正在创建 pdf 文件。下面是使用的代码:
更新完整代码
#include <QCoreApplication>
#include <QPrinter>
#include <QTextDocument>
int main(int argc, char *argv[])
{
QCoreApplication a(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("../out.pdf");
printer.setOutputFormat(QPrinter::PdfFormat);
doc.print(&printer);
printer.newPage();
return 0;
}