QPainter.drawText() SIGSEGV 分段错误 [英] QPainter.drawText() SIGSEGV Segmentation fault

查看:31
本文介绍了QPainter.drawText() SIGSEGV 分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Qt5 打印方法在热敏打印机中打印一条简单的文本消息.

I'm trying to print a simple text message in a thermal printer through Qt5 printing methods.

#include <QCoreApplication>
#include <QDebug>
#include <QtPrintSupport/QPrinterInfo>
#include <QtPrintSupport/QPrinter>
#include <QtGui/QPainter>

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);

   QPrinter printer(QPrinter::ScreenResolution);
   QPainter painter;
   painter.begin(&printer);
   painter.setFont(QFont("Tahoma",8));
   painter.drawText(0,0,"Test");
   painter.end();

   return a.exec();
}

但是,当我通过调试器运行它时,我在 drawText 方法上收到一个 SIGSEGV Segmentation fault 信号.

However when I run it through the debugger I get a SIGSEGV Segmentation fault signal on the drawText method.

打印机已连接,已安装,当我调用 qDebug() <<printer.printerName(); 我得到了应该使用的打印机的正确名称.

The printer is connected, installed and when I call qDebug() << printer.printerName(); I get the correct name of the printer that should be used.

有谁知道为什么会抛出这个错误SIGSEGV Segmentation fault"?

Anyone knows why is this error being thrown "SIGSEGV Segmentation fault"?

谢谢.

推荐答案

要使 QPrinter 工作,您需要一个 QGuiApplication,而不是 QCoreApplication.

For QPrinter to work you need a QGuiApplication, not a QCoreApplication.

这记录在 QPaintDevice 文档中:

警告: Qt 要求 QGuiApplication 对象存在,然后才能创建任何绘制设备.绘图设备访问窗口系统资源,这些资源在创建应用程序对象之前不会被初始化.

Warning: Qt requires that a QGuiApplication object exists before any paint devices can be created. Paint devices access window system resources, and these resources are not initialized before an application object is created.

请注意,至少在基于 Linux 的系统上,offscreen QPA 在这里不起作用.

Note that at least on Linux-based systems the offscreen QPA will not work here.

#include <QCoreApplication>
#include <QDebug>
#include <QtPrintSupport/QPrinterInfo>
#include <QtPrintSupport/QPrinter>
#include <QtGui/QPainter>
#include <QGuiApplication>
#include <QTimer>

int main(int argc, char *argv[])
{
  QGuiApplication a(argc, argv);

  QPrinter printer;//(QPrinter::ScreenResolution);

  // the initializer above is not the crash reason, i just don't
  // have a printer
  printer.setOutputFormat(QPrinter::PdfFormat);
  printer.setOutputFileName("nw.pdf");

  Q_ASSERT(printer.isValid());

  QPainter painter;
  painter.begin(&printer);
  painter.setFont(QFont("Tahoma",8));
  painter.drawText(0,0,"Test");
  painter.end();

  QTimer::singleShot(0, QCoreApplication::instance(), SLOT(quit()));

  return a.exec();
}

这篇关于QPainter.drawText() SIGSEGV 分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆