3

I have an image in uint8_t buffer and I am trying to use QImage as a wrapper to write text on the image. I have used drawLine() with no issues, but drawText() crashes the program. The below code is part of a boost thread in which I want to write text unto each image as it iterates through the function. Are there any bugs in Qt I am unaware of?

uint8_t *frameBuffer; // this contains image pixels
QImage img(frameBuffer, sizeX, m_sizeY, QImage::Format_RGB888);

QPainter p(&img);

p.setPen(QPen(Qt::green));
p.setFont(QFont("Times", 10, QFont::Bold));
p.drawLine(img.rect().bottomLeft().x(), img.rect().bottomLeft().y()-10,
           img.rect().bottomRight().x(), img.rect().bottomRight().y()-10);  //works!

p.drawText(img.rect(), Qt::AlignCenter, "Help");  //crashes program
JonnyCplusplus
  • 881
  • 3
  • 12
  • 22

2 Answers2

5

My project was set to a QCoreApplication (I had no GUI). Changing it to QApplication did the trick!

JonnyCplusplus
  • 881
  • 3
  • 12
  • 22
1

Just a guess... (I've never seen this error before, but have had other font issues on threads.)

Font rendering on background threads can be a little flaky in Qt, depending on how it was compiled. Check the value of QFontDatabase::supportsThreadedFontRendering on your system.

Note the documentation:

Returns true if font rendering is supported outside the GUI thread, false otherwise. In other words, a return value of false means that all QPainter::drawText() calls outside the GUI thread will not produce readable output.

Dave Mateer
  • 17,608
  • 15
  • 96
  • 149