簡體   English   中英

QPrinter運行時錯誤

[英]QPrinter runtime error

編輯:我設法得到一個編譯且不崩潰的版本。 剩下的唯一事情就是獲得所需的輸出,但是已經回答了這個特定問題(為什么崩潰),所以我要關閉問題。 我將在失效代碼之前發布工作代碼。

美好的一天! 我正在嘗試創建一個小的示例,該示例將僅創建pdf文檔。 一切都會編譯,但是在程序啟動時,它只會崩潰。 我正在使用Qt版本5.0.0

-新的工作代碼-

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\\Cutie\\PDFPrintMaybe");
   printer.setOutputFormat(QPrinter::PdfFormat);
   doc.print(&printer);
   printer.newPage();

  return 0;
}

這是項目代碼:

#-------------------------------------------------
#
# 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

----帶有錯誤的舊代碼---這是主要的cpp:

#include <QCoreApplication>
#include <QTextDocument>
#include <QPrinter>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QTextDocument doc;
    doc.setHtml("<h1>Testing, testing, is this thing on?!</h1>");
    QPrinter printer;
    printer.setOutputFileName("C:\\Users\\SameTime\\Desktop\\Cutie\\PDFPrintMaybe");
    printer.setOutputFormat(QPrinter::PdfFormat);
    doc.print(&printer);
    printer.newPage();
    return a.exec();
}

我有點無所適從,因為它正在編譯,但是在運行時(幾乎)立即崩潰。

嘗試在堆上創建對象,否則當它們超出范圍時它們會被自動刪除,然后框架可能會嘗試再次刪除它們並崩潰。

QTextDocument *doc = new QTextDocument();
doc->setHtml("<h1>Testing, testing, is this thing on?!</h1>");
QPrinter* printer = new QPrinter();
printer->setOutputFileName("C:\\Users\\SameTime\\Desktop\\Cutie\\PDFPrintMaybe");
printer->setOutputFormat(QPrinter::PdfFormat);
doc->print(printer);
printer->newPage();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM