Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Looking for a tutorial

    General and Desktop
    2
    5
    983
    Loading More Posts
    • 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.
    • P
      Pippin last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        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.

        1 Reply Last reply Reply Quote 1
        • P
          Pippin last edited by

          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.)

          1 Reply Last reply Reply Quote 0
          • Chris Kawa
            Chris Kawa Moderators last edited by

            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).

            P 1 Reply Last reply Reply Quote 1
            • P
              Pippin @Chris Kawa last edited by

              @Chris-Kawa

              Thank you so much!

              1 Reply Last reply Reply Quote 0
              • First post
                Last post