Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QT4 – Font rendering problem when using QBitmap
Forum Update on Monday, May 27th 2025

QT4 – Font rendering problem when using QBitmap

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
2 Posts 2 Posters 416 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hariprasasth
    wrote on last edited by
    #1

    Hello,

    I am developing a application for my embedded device using QT4 with Freetype font library.
    I have to create an image to display it on Monochrome display. Therefore, I created instances of QBitmap and QPainter.
    When I draw the text, the text rendering (look) is different. Some part in the characters are quite dark.
    Below is my test code:

    class QBitmapTest
    {
    public:
    QBitmapTest();
    bool Text(wchar_t* f_pszFontName,int f_nXPos, int f_nYPos, int f_nWidth, int f_nHeight,int f_nFontHeight);
    void saveAsImage();
    private:
    QBitmap* m_hBitmap;
    QPen m_hPen;
    QBrush m_hBrush;
    QPainter* m_hdcMem;
    };
    BitmapTest::QBitmapTest()
    {
    m_hBitmap = new QBitmap(500,500);
    m_hdcMem = new QPainter();
    m_hBitmap->fill(Qt::color1);
    if(m_hBitmap->isNull()!= true){
    m_hdcMem->begin(m_hBitmap);
    }
    printf("Painter status: Dc %d ", m_hdcMem->isActive());
    m_hPen.setWidth(1);
    m_hPen.setColor(Qt::color0);
    m_hPen.setStyle(Qt::SolidLine);
    m_hBrush.setColor(Qt::color0);
    m_hBrush.setStyle(Qt::SolidPattern);
    //m_hdcMem->setBackgroundMode(Qt::OpaqueMode);
    m_hdcMem->setPen(m_hPen);
    m_hdcMem->setBrush(m_hBrush);
    }

    bool QBitmapTest::Text(wchar_t* f_pwFontName,int f_nXPos, int f_nYPos, int f_nWidth, int f_nHeight,int f_nFontHeight)
    {
    QFont font;
    // font.setStyleStrategy(QFont::NoFontMerging);
    // font.setStyleStrategy(QFont::PreferQuality);
    // font.setStretch(QFont::SemiExpanded);
    // font.setHintingPreference(QFont::PreferNoHinting);

    if(f_pwFontName){
        QString fontName = QString::fromWCharArray(f_pwFontName);
        font.setFamily(fontName);
        font.setWeight(QFont::Normal);
        font.setPixelSize(f_nFontHeight);
    
        //  font.setPointSize(f_nFontHeight);
        //  font.setFixedPitch(true);
        //  font.setRawMode(true);
        //  m_hDC->setFont(font);
        qDebug() << "Font match" << font.exactMatch();
        QFontInfo chk(font);
        qDebug() << "Font match fi" << chk.exactMatch();
        qDebug() << "Font famly fi" << chk.family();
        qDebug() << "Font fixed pitch fi" << chk.fixedPitch();
        qDebug() << "Font point size fi" << chk.pointSize();
    
        m_hdcMem->setFont(font);
    }
    
    QString str("ABCDEF GHIJK LMNOP QRST WXYZ");
    QRect rect;
    rect.setLeft(f_nXPos);
    rect.setTop(f_nYPos);
    rect.setWidth(f_nWidth);
    rect.setHeight(f_nHeight);
    qDebug() << "Drawing";
    m_hdcMem->drawText(rect,str);
    return true;}
    

    void QBitmapTest::saveAsImage()
    {
    QImage image = m_hBitmap->toImage();
    image.save("test.bmp");
    }

    But when I change QBitmap to QPixmap, I don’t see such rendering issue. If I use QPixmap, I have to convert the pixels data (16 bpp) to monochrome image to display it.

    Font name: Roboto Mono
    Line1 – Font size: 10
    Line2 – Font size: 12
    Line3 – Font size: 13
    Line4 – Font size: 15
    Line5 – Font size: 17

    Image with QBitmap:
    See Line4 (E, F, L, M, NP, Q, R) 5 (B,D,E,J,K,L,P,R,T) and 6(M,N): Some part of characters are quite dark.
    0_1564481062636_1b53f2a5-c48b-4a6d-9082-9322889701cf-image.png
    Image with QPixmap:
    0_1564481092590_9f608328-4761-4327-a1e2-147d997d9863-image.png

    How can I resolve the rendering problem with QBitmap?

    Thanks,
    Hari

    M 1 Reply Last reply
    0
    • H Hariprasasth

      Hello,

      I am developing a application for my embedded device using QT4 with Freetype font library.
      I have to create an image to display it on Monochrome display. Therefore, I created instances of QBitmap and QPainter.
      When I draw the text, the text rendering (look) is different. Some part in the characters are quite dark.
      Below is my test code:

      class QBitmapTest
      {
      public:
      QBitmapTest();
      bool Text(wchar_t* f_pszFontName,int f_nXPos, int f_nYPos, int f_nWidth, int f_nHeight,int f_nFontHeight);
      void saveAsImage();
      private:
      QBitmap* m_hBitmap;
      QPen m_hPen;
      QBrush m_hBrush;
      QPainter* m_hdcMem;
      };
      BitmapTest::QBitmapTest()
      {
      m_hBitmap = new QBitmap(500,500);
      m_hdcMem = new QPainter();
      m_hBitmap->fill(Qt::color1);
      if(m_hBitmap->isNull()!= true){
      m_hdcMem->begin(m_hBitmap);
      }
      printf("Painter status: Dc %d ", m_hdcMem->isActive());
      m_hPen.setWidth(1);
      m_hPen.setColor(Qt::color0);
      m_hPen.setStyle(Qt::SolidLine);
      m_hBrush.setColor(Qt::color0);
      m_hBrush.setStyle(Qt::SolidPattern);
      //m_hdcMem->setBackgroundMode(Qt::OpaqueMode);
      m_hdcMem->setPen(m_hPen);
      m_hdcMem->setBrush(m_hBrush);
      }

      bool QBitmapTest::Text(wchar_t* f_pwFontName,int f_nXPos, int f_nYPos, int f_nWidth, int f_nHeight,int f_nFontHeight)
      {
      QFont font;
      // font.setStyleStrategy(QFont::NoFontMerging);
      // font.setStyleStrategy(QFont::PreferQuality);
      // font.setStretch(QFont::SemiExpanded);
      // font.setHintingPreference(QFont::PreferNoHinting);

      if(f_pwFontName){
          QString fontName = QString::fromWCharArray(f_pwFontName);
          font.setFamily(fontName);
          font.setWeight(QFont::Normal);
          font.setPixelSize(f_nFontHeight);
      
          //  font.setPointSize(f_nFontHeight);
          //  font.setFixedPitch(true);
          //  font.setRawMode(true);
          //  m_hDC->setFont(font);
          qDebug() << "Font match" << font.exactMatch();
          QFontInfo chk(font);
          qDebug() << "Font match fi" << chk.exactMatch();
          qDebug() << "Font famly fi" << chk.family();
          qDebug() << "Font fixed pitch fi" << chk.fixedPitch();
          qDebug() << "Font point size fi" << chk.pointSize();
      
          m_hdcMem->setFont(font);
      }
      
      QString str("ABCDEF GHIJK LMNOP QRST WXYZ");
      QRect rect;
      rect.setLeft(f_nXPos);
      rect.setTop(f_nYPos);
      rect.setWidth(f_nWidth);
      rect.setHeight(f_nHeight);
      qDebug() << "Drawing";
      m_hdcMem->drawText(rect,str);
      return true;}
      

      void QBitmapTest::saveAsImage()
      {
      QImage image = m_hBitmap->toImage();
      image.save("test.bmp");
      }

      But when I change QBitmap to QPixmap, I don’t see such rendering issue. If I use QPixmap, I have to convert the pixels data (16 bpp) to monochrome image to display it.

      Font name: Roboto Mono
      Line1 – Font size: 10
      Line2 – Font size: 12
      Line3 – Font size: 13
      Line4 – Font size: 15
      Line5 – Font size: 17

      Image with QBitmap:
      See Line4 (E, F, L, M, NP, Q, R) 5 (B,D,E,J,K,L,P,R,T) and 6(M,N): Some part of characters are quite dark.
      0_1564481062636_1b53f2a5-c48b-4a6d-9082-9322889701cf-image.png
      Image with QPixmap:
      0_1564481092590_9f608328-4761-4327-a1e2-147d997d9863-image.png

      How can I resolve the rendering problem with QBitmap?

      Thanks,
      Hari

      M Offline
      M Offline
      mvuori
      wrote on last edited by
      #2

      Of course your QPixmap looks good - it is a grayscale image which has many bits per pixel allowing nice antialiasing of the letters. That is not possible with a 1 bit bitmap. Convert the QPixmap to a bitmap and you'll see similar result.

      The thing to do is to find a font that works well (or at least better) on 1 bit screens.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved