Results 1 to 4 of 4

Thread: Font size calculation when painting in a QImage

  1. #1
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Font size calculation when painting in a QImage

    Hello,
    I'm trying to generate a modified image starting from a preexisting PNG file and drawing some text on it. I'm not getting what I think I should get
    In particular, I have some issues with font sizes.
    I have found that the PNG resolution plays a role in the calculation of the actual pixel size of the font, if I increase the resolution and keep the font size fixed, the actual rendered text is smaller. The sizes do not match what I see if I do the same in inkscape, but this is not a problem. My problem is that there seems to be a "minimum pixel size" which limits the size of the smallest font I can draw. Right now I have the PNG at 72x72 dpi and a font size of 2 looks identical to font size 10.

    The code:
    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication a(argc, argv);
    4. QImage* card = new QImage("blank72.png");
    5. QPainter* painter = new QPainter(card);
    6. QFont* FontText = new QFont("Serif", 2, QFont::Normal);
    7. QFont* FontText2 = new QFont("Serif", 10, QFont::Normal);
    8. painter->setRenderHint(QPainter::TextAntialiasing, true);
    9. painter->setPen(Qt::black);
    10. painter->setFont(*FontText);
    11. painter->drawText(QRect(0,0,256,128), Qt::TextWordWrap, "Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt");
    12. painter->setFont(*FontText2);
    13. painter->drawText(QRect(0,128,256,128), Qt::TextWordWrap, "Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt Qt");
    14. QLabel* label = new QLabel(0);
    15. label->setPixmap(QPixmap::fromImage(*card));
    16. label->show();
    17.  
    18. return a.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 

    blank72.png is a 256x256 white image with 72dpi resolution.

    Side question: is there any easy way to have the text justified?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Font size calculation when painting in a QImage

    Remember that you can set not only the point size for the font, but also the pixel size (which is dependent on the resolution).

  3. #3
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Font size calculation when painting in a QImage

    There still seem to be some kind of "lower limit", even when using setPixelSize.
    I've resorted to rendering in a separate image and then drawing this image while scaling it down. Of course it's not very readable, but this is not a problem since the text is more "decoration" than other.

    Is there a way to control the lineskip to draw the text lines closer or again it's easier to draw into intermediate images and build the text in blocks?

    Thanks in advance.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Font size calculation when painting in a QImage

    I guess you'd have to use QTextLayout or implement your own QAbstractDocumentLayout implementation.

Similar Threads

  1. Replies: 3
    Last Post: 30th January 2007, 08:35
  2. QTreeView font size not being set
    By forrestfsu in forum Qt Tools
    Replies: 2
    Last Post: 12th October 2006, 14:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.