Why this program crashed?
-
Hi
I encountered a problem that confused me. When I debug the program,it crashed.

int main(int argc, char* argv[]) { QCoreApplication a(argc, argv); QImage dfs("1.png"); QPainter painter; bool ret = painter.begin(&dfs); auto ddd = dfs.paintEngine(); if (!dfs.isNull() && painter.isActive()) painter.drawText(100, 100, QString("1")); return a.exec(); }the qt version is 5.14.2.
Regrads!
-
Hi
I encountered a problem that confused me. When I debug the program,it crashed.

int main(int argc, char* argv[]) { QCoreApplication a(argc, argv); QImage dfs("1.png"); QPainter painter; bool ret = painter.begin(&dfs); auto ddd = dfs.paintEngine(); if (!dfs.isNull() && painter.isActive()) painter.drawText(100, 100, QString("1")); return a.exec(); }the qt version is 5.14.2.
Regrads!
@John-Van said in Why this program crashed?:
bool ret = painter.begin(&dfs);
You do not check whether the image is null or not before you call begin(). I guess it is null because you use a relative path to the image so it is not found...
-
@John-Van said in Why this program crashed?:
bool ret = painter.begin(&dfs);
You do not check whether the image is null or not before you call begin(). I guess it is null because you use a relative path to the image so it is not found...
@jsulm The QImage object exists (it's on the stack) but it may represent a "null" image for the reason you gave. The attempt to paint is guarded by a check for a "null" image, so this should not even be executed in that circumstance.
@John-Van Crashes here for me with an existing image (Qt 5 or 6).
The program is crashing because it needs the QFontDatabase to render text. The requires at least a QGuiApplication object. Under Qt 6 you get the useful warningQFontDatabase: Must construct a QGuiApplication before accessing QFontDatabasebefore the crash. -
@jsulm The QImage object exists (it's on the stack) but it may represent a "null" image for the reason you gave. The attempt to paint is guarded by a check for a "null" image, so this should not even be executed in that circumstance.
@John-Van Crashes here for me with an existing image (Qt 5 or 6).
The program is crashing because it needs the QFontDatabase to render text. The requires at least a QGuiApplication object. Under Qt 6 you get the useful warningQFontDatabase: Must construct a QGuiApplication before accessing QFontDatabasebefore the crash.@ChrisW67 said in Why this program crashed?:
but it may represent a "null" image for the reason you gave
That's what I was talking about
-
@jsulm The QImage object exists (it's on the stack) but it may represent a "null" image for the reason you gave. The attempt to paint is guarded by a check for a "null" image, so this should not even be executed in that circumstance.
@John-Van Crashes here for me with an existing image (Qt 5 or 6).
The program is crashing because it needs the QFontDatabase to render text. The requires at least a QGuiApplication object. Under Qt 6 you get the useful warningQFontDatabase: Must construct a QGuiApplication before accessing QFontDatabasebefore the crash. -
J John Van has marked this topic as solved on