QPainter cannot draw the font

  • Qt5.9.2 msvc2017 and Qt5.6.3 msvc2015
    os : win10 64bits

        int 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);
        QPainter p(&img);
        p.drawText(img.rect(), str);

    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)

  • Lifetime Qt Champion


    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.

  • Qt Champions 2017

    That font is not valid
    alt text
    it contains nothing at all.

    QImage img(metrics.boundingRect("0123456789").width(),
    might become ZERO width.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.