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. how do I rotate text about an arbitrary point?
Forum Updated to NodeBB v4.3 + New Features

how do I rotate text about an arbitrary point?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.1k Views 2 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.
  • P Offline
    P Offline
    PsylumDan
    wrote on last edited by PsylumDan
    #1

    0_1558617764886_Capture.PNG

    The image is the start of a compass display. It's also my first step into drawing on a QPainter.
    The line works fine. It rotates around the center of the image. I would like the text to rotate around the center point as well. As it is, the text is rotating around 0, 0 (upper left corner). The forum won't let me attach the zipped project so here is my paintEvent code. This is a subclass of a QLabel.

    void AZ_Label::paintEvent(QPaintEvent *ev)
    {
        Q_UNUSED(ev);
        QPainter p(this);
        p.setPen(QPen(Qt::white, 2));
        p.rotate(lineAngle);
        p.save();
        p.translate(translateX, translateY);
        p.drawText(50, 50, "North");
        p.restore();
        p.rotate(-lineAngle);
        p.drawEllipse(xPosition, yPosition, cSize, cSize);
        line.setAngle(qreal(lineAngle));
        p.drawLine(line);
        emit sigSizechanged(xPosition, yPosition, cSize);
    }
    

    translateX and translateY are set from a slider on the main window.

    JonBJ 1 Reply Last reply
    0
    • P PsylumDan

      0_1558617764886_Capture.PNG

      The image is the start of a compass display. It's also my first step into drawing on a QPainter.
      The line works fine. It rotates around the center of the image. I would like the text to rotate around the center point as well. As it is, the text is rotating around 0, 0 (upper left corner). The forum won't let me attach the zipped project so here is my paintEvent code. This is a subclass of a QLabel.

      void AZ_Label::paintEvent(QPaintEvent *ev)
      {
          Q_UNUSED(ev);
          QPainter p(this);
          p.setPen(QPen(Qt::white, 2));
          p.rotate(lineAngle);
          p.save();
          p.translate(translateX, translateY);
          p.drawText(50, 50, "North");
          p.restore();
          p.rotate(-lineAngle);
          p.drawEllipse(xPosition, yPosition, cSize, cSize);
          line.setAngle(qreal(lineAngle));
          p.drawLine(line);
          emit sigSizechanged(xPosition, yPosition, cSize);
      }
      

      translateX and translateY are set from a slider on the main window.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @PsylumDan
      Does https://stackoverflow.com/questions/31760995/how-to-set-transform-origin-for-qpainter-in-qt, https://stackoverflow.com/a/46180679 address your issue?

      P 1 Reply Last reply
      0
      • JonBJ JonB

        @PsylumDan
        Does https://stackoverflow.com/questions/31760995/how-to-set-transform-origin-for-qpainter-in-qt, https://stackoverflow.com/a/46180679 address your issue?

        P Offline
        P Offline
        PsylumDan
        wrote on last edited by
        #3

        @JonB said in how do I rotate text about an arbitrary point?:

        @PsylumDan
        Does https://stackoverflow.com/questions/31760995/how-to-set-transform-origin-for-qpainter-in-qt, https://stackoverflow.com/a/46180679 address your issue?

        Thanks for the reply but no. The line works as expected. It's the text that I'm having trouble with.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          Qt is not "holding" the text in its center.
          You have to calculate the center.
          Please try code here.
          https://stackoverflow.com/questions/40300346/qt-rotating-text-around-its-center-point

          P 1 Reply Last reply
          3
          • mrjjM mrjj

            Hi
            Qt is not "holding" the text in its center.
            You have to calculate the center.
            Please try code here.
            https://stackoverflow.com/questions/40300346/qt-rotating-text-around-its-center-point

            P Offline
            P Offline
            PsylumDan
            wrote on last edited by
            #5

            @mrjj said in how do I rotate text about an arbitrary point?:

            Hi
            Qt is not "holding" the text in its center.
            You have to calculate the center.
            Please try code here.
            https://stackoverflow.com/questions/40300346/qt-rotating-text-around-its-center-point

            That's exactly what I needed. Thanks!

            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