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. Can QPainter.drawText rotate text ?
QtWS25 Last Chance

Can QPainter.drawText rotate text ?

Scheduled Pinned Locked Moved Solved General and Desktop
c++ qtsvg
4 Posts 2 Posters 625 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.
  • G Offline
    G Offline
    Gilboonet
    wrote on 7 Jun 2023, 09:27 last edited by
    #1

    I'm exporting the output of my application to a svg file using QSvgGenerator and QPainter. It goes well for the lines and paths, but I also need to output rotated text and I didn't find anything that allow me do to that.
    Here is what I managed to do...
    deplUI6.png
    For the text output my application uses QGraphicsSimpleTextItem and setRotation() on it, and painter.drawText(). Is it possible to create a svg file having text tags with rotation transform or do I need to do that by myself ?

    G 1 Reply Last reply 7 Jun 2023, 10:01
    0
    • T TomZ
      7 Jun 2023, 10:47

      @Gilboonet yes, that is indeed the way to do it. Except that you should likely put the rotate and translate before the drawText

      G Offline
      G Offline
      Gilboonet
      wrote on 7 Jun 2023, 12:08 last edited by Gilboonet 6 Jul 2023, 12:09
      #4

      @TomZ Thank you, I follow the order you mentioned and it works fine.

      if (doSVG) {
          painter.setFont(fNum);
          painter.setPen(pNum);
          painter.save();
          painter.translate(c.toPointF()+ b.toPointF() +tit->pos());
          painter.rotate(radToDeg(ra));
          painter.drawText(- ti->boundingRect().width()/2, -2, QString::number(n));
          painter.restore();
      }
      

      deplUI6a.png

      1 Reply Last reply
      0
      • G Gilboonet
        7 Jun 2023, 09:27

        I'm exporting the output of my application to a svg file using QSvgGenerator and QPainter. It goes well for the lines and paths, but I also need to output rotated text and I didn't find anything that allow me do to that.
        Here is what I managed to do...
        deplUI6.png
        For the text output my application uses QGraphicsSimpleTextItem and setRotation() on it, and painter.drawText(). Is it possible to create a svg file having text tags with rotation transform or do I need to do that by myself ?

        G Offline
        G Offline
        Gilboonet
        wrote on 7 Jun 2023, 10:01 last edited by
        #2

        @Gilboonet Apparently I need to use

        painter.save();
        painter.drawText(0,0, string);
        painter.rotate();
        painter.translate(x, y);
        painter.restore();
        
        T 1 Reply Last reply 7 Jun 2023, 10:47
        0
        • G Gilboonet
          7 Jun 2023, 10:01

          @Gilboonet Apparently I need to use

          painter.save();
          painter.drawText(0,0, string);
          painter.rotate();
          painter.translate(x, y);
          painter.restore();
          
          T Offline
          T Offline
          TomZ
          wrote on 7 Jun 2023, 10:47 last edited by TomZ 6 Jul 2023, 10:48
          #3

          @Gilboonet yes, that is indeed the way to do it. Except that you should likely put the rotate and translate before the drawText

          G 1 Reply Last reply 7 Jun 2023, 12:08
          3
          • T TomZ
            7 Jun 2023, 10:47

            @Gilboonet yes, that is indeed the way to do it. Except that you should likely put the rotate and translate before the drawText

            G Offline
            G Offline
            Gilboonet
            wrote on 7 Jun 2023, 12:08 last edited by Gilboonet 6 Jul 2023, 12:09
            #4

            @TomZ Thank you, I follow the order you mentioned and it works fine.

            if (doSVG) {
                painter.setFont(fNum);
                painter.setPen(pNum);
                painter.save();
                painter.translate(c.toPointF()+ b.toPointF() +tit->pos());
                painter.rotate(radToDeg(ra));
                painter.drawText(- ti->boundingRect().width()/2, -2, QString::number(n));
                painter.restore();
            }
            

            deplUI6a.png

            1 Reply Last reply
            0
            • G Gilboonet has marked this topic as solved on 7 Jun 2023, 12:08

            2/4

            7 Jun 2023, 10:01

            • Login

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