首页 > 解决方案 > 为什么将文档打印到 PDF 会崩溃?

问题描述

我正在尝试从QtWiki运行使用 Qt 生成 pdf 的官方示例,但它会因代码而崩溃-1073740940。它发生在这一行:QPrinter printer(QPrinter::PrinterResolution);

printsupport添加到项目文件中。

我没有更改示例代码中的任何内容:

#include <QtWidgets>
#ifndef QT_NO_PRINTER
#include <QPrinter>
#endif

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

    QString fileName = QFileDialog::getSaveFileName((QWidget* )0, "Export PDF", QString(), "*.pdf");
    if (QFileInfo(fileName).suffix().isEmpty()) { fileName.append(".pdf"); }

    QPrinter printer(QPrinter::PrinterResolution);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setPaperSize(QPrinter::A4);
    printer.setOutputFileName(fileName);

    QTextDocument doc;
    doc.setHtml("<h1>Hello, World!</h1>\n<p>Lorem ipsum dolor sit amet, consectitur adipisci elit.</p>");
    doc.setPageSize(printer.pageRect().size()); // This is necessary if you want to hide the page number
    doc.print(&printer);
}

我在设备中有“Microsoft print to pdf”。

崩溃的原因可能是什么以及如何解决?

// 之后添加

我的设置:

Qt 5.14.1
MinGW 64 bit
Win 10 64 bit

调试器堆栈跟踪:

1  ntdll!RtlIsNonEmptyDirectoryReparsePointAllowed                                                    0x7ffa1b1b91b3 
2  ntdll!RtlpNtMakeTemporaryKey                                                                       0x7ffa1b1c15e2 
3  ntdll!RtlpNtMakeTemporaryKey                                                                       0x7ffa1b1c18ea 
4  ntdll!RtlpNtMakeTemporaryKey                                                                       0x7ffa1b1ca8a9 
5  ntdll!RtlGetCurrentServiceSessionId                                                                0x7ffa1b10253a 
6  ntdll!RtlGetCurrentServiceSessionId                                                                0x7ffa1b100790 
7  ntdll!RtlFreeHeap                                                                                  0x7ffa1b0ffb91 
8  ntdll!RtlpNtMakeTemporaryKey                                                                       0x7ffa1b1c53c9 
9  ntdll!memset                                                                                       0x7ffa1b175670 
10 ntdll!RtlGetCurrentServiceSessionId                                                                0x7ffa1b100790 
11 ntdll!RtlFreeHeap                                                                                  0x7ffa1b0ffb91 
12 msvcrt!free                                                                                        0x7ffa19189cfc 
13 QWindowsPrintDevice::defaultPageSize() const                                                       0x6f882e41     
14 QPrintDevice::defaultPageSize() const                                                              0x6df43efd     
15 QWin32PrintEngine::QWin32PrintEngine(QPrinter::PrinterMode, QString const&)                        0x6df528b2     
16 QWindowsPrinterSupport::createNativePrintEngine(QPrinter::PrinterMode, QString const&)             0x6f88169a     
17 QPrinterPrivate::initEngines(QPrinter::OutputFormat, QPrinterInfo const&)                          0x6df4a523     
18 QPrinterPrivate::init(QPrinterInfo const&, QPrinter::PrinterMode)                                  0x6df4a70e     
19 QPrinter::QPrinter(QPrinter::PrinterMode)                                                          0x6df4a818     
20 main                                                                                   main.cpp 13 0x4016cc       

标签: qtpdfprinttopdf

解决方案


这不是解决方案,而是一种解决方法。更改默认打印机。由于某些原因,当某些打印机是系统默认值时,QPrinter 构造函数会崩溃。


推荐阅读