Important: Please read the Qt Code of Conduct -

Looking for a tutorial

  • Hello,

    I'm looking for a nice Qt 5.5 tutorial that would show me how to put different texts with different sizes and different fonts on a QImage via a QPainter. And if possible, a tutorial on how to draw a -justified- text on a QImage.

    I've googled this but I haven't quite found what I needed. I apologize if I missed something obvious.

    Thank you.

  • Moderators

    To create a QPainter that will paint on a QImage simply pass the QImage as the parameter in the QPainter constructor.

    To change font of a QPainter use setFont().

    To draw a justified text use the drawText() method of a QPainter and pass Qt::AlignJustify in the flags argument of the method.

  • Thank you for your help, that works perfectly!

    May I also ask how to "stretch" the height of a text? There is QFont::setStretch(int) but that only works for width, and I would like to amplify the height.

    I've found a workaround with

    QPainterObject.setTransform(QTransform(1.0, 0, 0, factor, 0, 0));

    but that messes with positions. (If I want to draw a text at (100, 100) it will be drawn at (100, 100*factor) instead.)

  • Moderators

    You can only control the horizontal stretch, but this is enough.
    If you want to make the text stretch lets say 200% vertically then make it 2 times bigger using font.setPointSize(font.pointSize() * 2) and set the stretch to half using font.setStretch(50).

  • @Chris-Kawa

    Thank you so much!

Log in to reply