1

いくつかの例を見て、そのうちの 1 つを実装することにしました。コンパイルして実行してもクラッシュしませんが、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
4

1 に答える 1

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

おそらく、ファイル名ではなく、既存のフォルダーを参照します。

次のように、代わりに pdf ファイル名を指定する必要があります。

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

ファイルへのパスが存在し、アクセス可能であることを確認してください。

そうしないと、sustem は pdf をクレートして印刷できません (つまり、pdf キャンバスにペイントできません)。

于 2013-06-08T16:02:03.263 に答える