Qt创建pdf示例 - 无法使其工作

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

本文关键字:工作 创建 pdf 示例 Qt      更新时间:2023-10-16

我看了一些例子,决定实现其中一个。它可以编译并且在运行时不会崩溃,但是它不会创建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
"C:\Users\SameTime\Desktop"

可能是指现有文件夹,而不是文件名。

您应该指定您的 pdf 文件名,例如

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

并确保文件的路径存在且可访问。

否则,sustem将无法将pdf装箱并打印(即在pdf画布上绘画(