QPainter.drawText() SIGSEGV Segmentation fault(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
文档中有记录:
This is documented in QPaintDevice
docs:
警告: 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 分割错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:QPainter.drawText() SIGSEGV 分割错误


基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01