Solved QPainter cannot draw the font
-
Qt5.9.2 msvc2017 and Qt5.6.3 msvc2015
os : win10 64bitsint const id = QFontDatabase::addApplicationFont(font_name); QString family = QFontDatabase::applicationFontFamilies(id).at(0); qDebug()<<"string family:"<<family; QFont monospace(family, 32, 10, false); QFontMetrics metrics(monospace); QImage img(metrics.boundingRect("0123456789").width(), metrics.height(), QImage::Format_ARGB32); img.fill(QColor(0,0,0)); qDebug()<<img.size(); QPainter p(&img); p.setPen(QPen(Qt::green)); p.setFont(monospace); p.drawText(img.rect(), str); ui->labelImg->setPixmap(QPixmap::fromImage(img));
QPainter cannot pain the ttf text of google font, the font is at here.
Error messages:
QPainter::begin: Paint device returned engine == 0, type: 3 QPainter::setPen: Painter not active QPainter::setFont: Painter not active
The size of the QFontMetrics always give me QSize(1, 43)
-
Hi,
One thing I can spot, you didn't call
p.end()
before trying to transform the QImage to a QPixmap. -
@SGaist Thanks, I add p.end() before setPixmap, but it still give me the same asnwer.
-
Hi
That font is not valid
it contains nothing at all.So
QImage img(metrics.boundingRect("0123456789").width(),
might become ZERO width.