Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with painter.drawText, transform and translate
Forum Updated to NodeBB v4.3 + New Features

Problem with painter.drawText, transform and translate

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.4k Views 1 Watching
  • 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.
  • J Offline
    J Offline
    jh224
    wrote on last edited by
    #1

    All is well with the below code as long as both offsets are 0. This is even true when the text is rotated and mirrored. However when there are offsets, drawText is skewed when the rotation is not 0 degrees. I thought that QTransform did all the math? Is my implementation wrong? Do I need to do the math to compensate for the skewing?

    @
    void paintImage(QGraphicsTextItem &txtItem, int offsetX, int offsetY)
    {
    QPainter painter(&myImage);
    painter.setPen(myPen);
    painter.setFont(txtItem.font());

    QTransform trans = txtItem.sceneTransform();
    trans.translate(offsetX, offsetY);

    painter.setTransform(trans);

    painter.drawText(txtItem.boundingRect(), Qt::AlignCenter | Qt::AlignJustify, txtItem.toPlainText());
    }

    @

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      The order of transformations matters. For example translate => rotate => translate will give totally different results than translate => translate => rotate.

      It's not usually possible to gradually add transformations.
      I would store the desired offsets, scale and rotation and recreate transform matrix in the correct order each time any of those changes.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jh224
        wrote on last edited by
        #3

        Chris, Thanks for your response. You're right, the order does matter. After playing around with it for a little bit, the following is the solution that I came up with.

        @

        void paintImage(QGraphicsTextItem &txtItem, int offsetX, int offsetY)
        {
          QPainter painter(&myImage);
          painter.setPen(myPen);
          painter.setFont(txtItem.font());
         
         QTransform trans;
        trans.setMatrix(txtItem.sceneTransformation().m11(),
        

        txtItem.sceneTransformation().m12(), txtItem.sceneTransformation().m13(), txtItem.sceneTransformation().m21(), txtItem.sceneTransformation().m22(), txtItem.sceneTransformation().m23(), txtItem.sceneTransformation().m31()-offsetX, txtItem.sceneTransformation().m32()-offsetY, txtItem.sceneTransformation().m33());

        painter.setTransform(trans);

        painter.drawText(txtItem.boundingRect(), Qt::AlignCenter  |    Qt::AlignJustify, txtItem.toPlainText());
        }
        

        @

        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