[英]QPainter#drawText seg faults in initializeDb
Using Qt 5.7, Windows 7, MinGW 32-bit, the following program: 使用Qt 5.7,Windows 7,MinGW 32位以下程序:
#include <QImage>
#include <QPainter>
int main () {
QImage i(100, 100, QImage::Format_RGB888);
QPainter p(&i);
p.drawText(0, 0, "abc"); // line 7
}
Seg faults on the p.drawText
call, giving the following stack trace, which ends with initializeDb
: 在
p.drawText
调用上出现p.drawText
错误,给出以下堆栈跟踪,并以initializeDb
结尾:
1 initializeDb qfontdatabase.cpp 896 0x7930ed0
2 QFontDatabase::findFont qfontdatabase.cpp 2640 0x79361f6
3 QFontDatabase::load qfontdatabase.cpp 2795 0x7936b5e
4 QFontPrivate::engineForScript qfont.cpp 215 0x79194ff
5 QTextEngine::fontEngine qtextengine.cpp 2094 0x793d24b
6 QTextEngine::shapeText qtextengine.cpp 1000 0x7938c0b
7 QTextEngine::shape qtextengine.cpp 1534 0x793b090
8 QTextEngine::shapeLine qtextengine.cpp 938 0x793884a
9 QPainter::drawText qpainter.cpp 5877 0x7a3dc91
10 QPainter::drawText qpainter.cpp 5700 0x7a3cfe6
11 QPainter::drawText qpainter.h 890 0x402a1e
12 main main.cpp 7 0x4016b6
Why is this happening and how do I make it not happen? 为什么会发生这种情况,我该如何避免呢?
The .pro file, for completeness: .pro文件,出于完整性考虑:
QT += core gui
CONFIG += c++11
TARGET = untitled18
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
This is for a command line utility that generates images. 这是用于生成图像的命令行实用程序。
Note: Adding a QCoreApplication
does not make a difference. 注意:添加
QCoreApplication
并没有什么不同。
Looking at the Qt source code can help with problems like this. 查看Qt源代码可以帮助解决此类问题。
Line 896 of qfontdatabase.cpp (where your stack trace shows the crash occurring at) looks like this: qfontdatabase.cpp的第896行(堆栈跟踪显示发生崩溃的位置)如下所示:
QGuiApplicationPrivate::platformIntegration()->fontDatabase()->populateFontDatabase();
.... so most likely either platformIntegration() or fontDatabase() is returning NULL for some reason. ....因此,出于某种原因,platformIntegration()或fontDatabase()最有可能返回NULL。
Grepping through the source code we see that QGuiApplicationPrivate::platformIntegration() is defined here, at line 103 of gui/kernel/qguiapplication.h: 仔细查看源代码,我们看到在gui / kernel / qguiapplication.h的第103行中定义了QGuiApplicationPrivate :: platformIntegration():
static QPlatformIntegration *platformIntegration()
{ return platform_integration; }
... so that method could definitely return NULL if the platform_integration variable hasn't been set yet to point to any valid object yet. ...因此,如果尚未将platform_integration变量设置为指向任何有效对象,则该方法肯定可以返回NULL。
With just a bit more grepping around, we find that the only place where the platform_integration static variable is set is at line 1094 of gui/kernel/qguiapplication.cpp: 经过一番摸索,我们发现设置platform_integration静态变量的唯一位置是gui / kernel / qguiapplication.cpp的第1094行:
QGuiApplicationPrivate::platform_integration = QPlatformIntegrationFactory::create(name, arguments, argc, argv, platformPluginPath);
... which is part of a static function called init_platform(), which is called from QGuiApplicationPrivate::createPlatformIntegration(), which itself is called from various methods of the QGuiApplicationPrivate class. ...是称为init_platform()的静态函数的一部分,该函数从QGuiApplicationPrivate :: createPlatformIntegration()调用,该函数本身从QGuiApplicationPrivate类的各种方法中调用。
But of course none of the QGuiApplicationPrivate methods can be called unless/until a QGuiApplicationPrivate object is created, which presumably won't happen unless/until you've created a QGuiApplication object. 但是,当然,除非/直到创建QGuiApplicationPrivate对象,否则才能调用QGuiApplicationPrivate方法,除非/直到您创建了QGuiApplication对象,否则不会发生。
So to sum up... it looks like Rinold is correct, you need to instantiate a QGuiApplication (or QApplication, which is a subclass of QGuiApplication) object first, before trying to use a QPainter to draw text. 因此,总结起来……看起来Rinold是正确的,您需要先实例化一个QGuiApplication(或QApplication,它是QGuiApplication的子类)对象,然后再尝试使用QPainter绘制文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.