【问题标题】:Why printing document to PDF crashes?为什么将文档打印到 PDF 会崩溃?
【发布时间】:2020-08-28 17:50:13
【问题描述】:

我正在尝试从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       

【问题讨论】:

  • 异常代码 0xc0000374 表示堆损坏 - 要确定解决方案,您需要在调试器中调试崩溃以确定谁在损坏堆。
  • 我无法重现崩溃。完全相同的代码,不是来自示例,而是您发布的代码,在 Windows 10 下运行良好。
  • 我已经测试了您发布的代码,它运行时没有任何崩溃。也许您没有为要创建的 pdf 文件选择的文件夹的写入权限。我不确定是不是这个原因,但您可以尝试选择另一个文件夹来确定。
  • 感谢您的建议。我试过用这段代码从头开始创建新项目,结果是一样的。我拥有在相应文件夹中写入文件的所有权限。
  • @AlexanderLyapin 你应该指出你环境的特点:操作系统、Qt版本、编译器等。可能是特定版本的bug。

标签: qt pdf printtopdf


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2010-09-16
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 2019-08-15
    • 2020-11-21
    • 1970-01-01
    相关资源
    最近更新 更多