首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QPrinter运行时错误

QPrinter运行时错误
EN

Stack Overflow用户
提问于 2013-06-08 15:14:46
回答 1查看 630关注 0票数 0

编辑:我设法得到了一个编译和非崩溃的版本。剩下的唯一一件事就是获得所需的输出,但是这个特定的问题(为什么它崩溃)已经得到了答案,所以我结束了这个问题。我将在损坏的代码之前发布工作代码。

日安!我正在尝试创建一个小示例,将简单地创建一个pdf文档。一切都会编译,但是当程序启动时,它就会崩溃。我使用的是Qt版本5.0.0

-新的工作代码

代码语言:javascript
复制
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;
}

项目代码如下:

代码语言:javascript
复制
#-------------------------------------------------
#
# 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:

代码语言:javascript
复制
#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();
}

我有点不知所措,因为它正在编译,但在运行时(几乎)立即崩溃。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-08 15:32:11

尝试在堆上创建对象,否则它们会在超出范围时自动删除,然后框架可能会再次尝试删除它们并崩溃。

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16997082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档