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 to rotate draw text only in QPainter
Forum Updated to NodeBB v4.3 + New Features

How to rotate draw text only in QPainter

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 3.9k 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.
  • Ramkumar MohanR Offline
    Ramkumar MohanR Offline
    Ramkumar Mohan
    wrote on last edited by Ramkumar Mohan
    #1

    How to rotate text only in QPainter.

    My code :

      void MainWindow::on_pushButton_clicked()
      {
          QPrinter *printer=new QPrinter(QPrinter::HighResolution);
          printer->setPrinterName("POSTEK C18/500+ #2");
          printer->setPaperSize(QSizeF(3.35, 0.39), QPrinter::Inch);   
    
          QImage imge("E:/QT Offline/Qt5.12.12/Barcode/build-Barcode- 
          Desktop_Qt_5_12_12_MinGW_64_bit-Debug/img1.png");
    
          QPainter painter1;
          painter1.begin(printer);
          painter1.setFont(QFont("times",40 , QFont::Bold));
          double xscale = printer->pageRect().width() / double(imge.width());
          double yscale = printer->pageRect().height() / double(imge.height());
          double scale = qMin(xscale, yscale);
          painter1.translate(printer->paperRect().x() + printer->pageRect().width() / 2,
                             printer->paperRect().y() + printer->pageRect().height() / 2);
          painter1.scale(scale, scale);
          QFont font = painter1.font() ;
          QString str = "MENU";
          QString str2= "EXIT";
    
          painter1.drawText(-2800,-330 ,str);
           painter1.drawText(+2800,-330 ,str2);
          painter1.drawImage(-1000 ,-280, imge);
          painter1.end();
     }
    

    My output is,

    da copy1.jpg

    How to set up, I want to set up as per below image.

    da copy.jpg

    Thanks in advance..

    JonBJ 1 Reply Last reply
    0
    • Ramkumar MohanR Ramkumar Mohan

      How to rotate text only in QPainter.

      My code :

        void MainWindow::on_pushButton_clicked()
        {
            QPrinter *printer=new QPrinter(QPrinter::HighResolution);
            printer->setPrinterName("POSTEK C18/500+ #2");
            printer->setPaperSize(QSizeF(3.35, 0.39), QPrinter::Inch);   
      
            QImage imge("E:/QT Offline/Qt5.12.12/Barcode/build-Barcode- 
            Desktop_Qt_5_12_12_MinGW_64_bit-Debug/img1.png");
      
            QPainter painter1;
            painter1.begin(printer);
            painter1.setFont(QFont("times",40 , QFont::Bold));
            double xscale = printer->pageRect().width() / double(imge.width());
            double yscale = printer->pageRect().height() / double(imge.height());
            double scale = qMin(xscale, yscale);
            painter1.translate(printer->paperRect().x() + printer->pageRect().width() / 2,
                               printer->paperRect().y() + printer->pageRect().height() / 2);
            painter1.scale(scale, scale);
            QFont font = painter1.font() ;
            QString str = "MENU";
            QString str2= "EXIT";
      
            painter1.drawText(-2800,-330 ,str);
             painter1.drawText(+2800,-330 ,str2);
            painter1.drawImage(-1000 ,-280, imge);
            painter1.end();
       }
      

      My output is,

      da copy1.jpg

      How to set up, I want to set up as per below image.

      da copy.jpg

      Thanks in advance..

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

      @Ramkumar-Mohan
      My guess would be call QPainter::rotate() before the drawText, undo/restore before drawImage().
      Plenty of web examples from Google QPainter rotate text, why not try them first and ask here if you get stuck?

      Ramkumar MohanR 1 Reply Last reply
      1
      • JonBJ JonB

        @Ramkumar-Mohan
        My guess would be call QPainter::rotate() before the drawText, undo/restore before drawImage().
        Plenty of web examples from Google QPainter rotate text, why not try them first and ask here if you get stuck?

        Ramkumar MohanR Offline
        Ramkumar MohanR Offline
        Ramkumar Mohan
        wrote on last edited by
        #3

        @JonB

        I tried the qpainter::rotate function but everything on the page is rotated.

        JonBJ 1 Reply Last reply
        0
        • Ramkumar MohanR Ramkumar Mohan

          @JonB

          I tried the qpainter::rotate function but everything on the page is rotated.

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

          @Ramkumar-Mohan
          And what did you do about the

          undo/restore before drawImage().

          Please don't tell me you did nothing.

          So, did you compare your code against the examples you got from Google QPainter rotate text? Are you saying those examples do not work? Or what are you asking us?

          Ramkumar MohanR 1 Reply Last reply
          0
          • JonBJ JonB

            @Ramkumar-Mohan
            And what did you do about the

            undo/restore before drawImage().

            Please don't tell me you did nothing.

            So, did you compare your code against the examples you got from Google QPainter rotate text? Are you saying those examples do not work? Or what are you asking us?

            Ramkumar MohanR Offline
            Ramkumar MohanR Offline
            Ramkumar Mohan
            wrote on last edited by
            #5

            @JonB

            Now , my code is,

            for (int i=0; i<1;++i )
              {
                  QThread::msleep(1000);
                  QImage imge("E:/QT Offline/Qt5.12.12/Barcode/build-Barcode- 
                  Desktop_Qt_5_12_12_MinGW_64_bit-Debug/img1.png");
                  QPainter painter1;
                  painter1.begin(printer);
                  painter1.setFont(QFont("times",40 , QFont::Bold));
                  double xscale = printer->pageRect().width() / double(imge.width());
                  double yscale = printer->pageRect().height() / double(imge.height());
                  double scale = qMin(xscale, yscale);
                  painter1.translate(printer->paperRect().x() + printer->pageRect().width() / 2,
                                     printer->paperRect().y() + printer->pageRect().height() / 2);
                  painter1.scale(scale, scale);
                  QFont font = painter1.font() ;
                  QString str = "MENU";
                  QString str2 = "EXIT";
                  painter1.save();
                  painter1.rotate(-90);
                  painter1.drawText(-2500,-150,str);
                  painter1.drawText(+2800,-150 ,str2);
                  painter1.restore();
                  painter1.drawImage(-1000 ,-280, imge);
                  painter1.end();
              }
            

            but text not printed ,

            WhatsApp Image 2023-01-25 at 7.36.20 PM.jpeg

            I don't know where i'm doing wrong.

            Thanks,

            JonBJ 1 Reply Last reply
            0
            • Ramkumar MohanR Ramkumar Mohan

              @JonB

              Now , my code is,

              for (int i=0; i<1;++i )
                {
                    QThread::msleep(1000);
                    QImage imge("E:/QT Offline/Qt5.12.12/Barcode/build-Barcode- 
                    Desktop_Qt_5_12_12_MinGW_64_bit-Debug/img1.png");
                    QPainter painter1;
                    painter1.begin(printer);
                    painter1.setFont(QFont("times",40 , QFont::Bold));
                    double xscale = printer->pageRect().width() / double(imge.width());
                    double yscale = printer->pageRect().height() / double(imge.height());
                    double scale = qMin(xscale, yscale);
                    painter1.translate(printer->paperRect().x() + printer->pageRect().width() / 2,
                                       printer->paperRect().y() + printer->pageRect().height() / 2);
                    painter1.scale(scale, scale);
                    QFont font = painter1.font() ;
                    QString str = "MENU";
                    QString str2 = "EXIT";
                    painter1.save();
                    painter1.rotate(-90);
                    painter1.drawText(-2500,-150,str);
                    painter1.drawText(+2800,-150 ,str2);
                    painter1.restore();
                    painter1.drawImage(-1000 ,-280, imge);
                    painter1.end();
                }
              

              but text not printed ,

              WhatsApp Image 2023-01-25 at 7.36.20 PM.jpeg

              I don't know where i'm doing wrong.

              Thanks,

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

              @Ramkumar-Mohan
              My last word on this, since I have never done it. After rotation, are you sure the (x, y) coordinates for drawText() don't need altering? Are you now drawing, say, outside the visible area?

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

                rotate does not rotate the following draw operation. It rotates the coordinate space of the painter i.e. the "canvas" on which you draw. It is also not rotated around arbitrary center of the following operation's argument (like text or image). It is always rotated around the (0,0) point in that space.
                Since you're doing a translation to the center the rotation axis is the center of the page.

                So rotate(-90) basically rotates your "page" around the center of the page. If you then draw at -2500,-150 it's out of the page (somewhere below the bottom border I'm guessing).

                What you need to do is first move the coordinate system so that the center is where the center of the text should be (use translate for that). Next, rotate the canvas using rotate. After that you can translate back and draw the text at (0,0) or pass the back translation to the drawing function directly.

                See this thread for example of such transformation.

                Btw. Why do you have QThread::msleep(1000); in a loop? That will freeze your UI and is not a good experience.

                1 Reply Last reply
                2
                • J Offline
                  J Offline
                  James Gallegos
                  wrote on last edited by
                  #8

                  @Ramkumar-Mohan said in How to rotate draw text only in QPainter:

                  How to rotate text only in QPainter.
                  My code :
                  void MainWindow::on_pushButton_clicked()
                  {
                  QPrinter *printer=new QPrinter(QPrinter::HighResolution);
                  printer->setPrinterName("POSTEK C18/500+ #2");
                  printer->setPaperSize(QSizeF(3.35, 0.39), QPrinter::Inch);

                    QImage imge("E:/QT Offline/Qt5.12.12/Barcode/build-Barcode- 
                    Desktop_Qt_5_12_12_MinGW_64_bit-Debug/img1.png");
                  
                    QPainter painter1;
                    painter1.begin(printer);
                    painter1.setFont(QFont("times",40 , QFont::Bold));
                    double xscale = printer->pageRect().width() / double(imge.width());
                    double yscale = printer->pageRect().height() / double(imge.height());
                    double scale = qMin(xscale, yscale);
                    painter1.translate(printer->paperRect().x() + printer->pageRect().width() / 2,
                                       printer->paperRect().y() + printer->pageRect().height() / 2);
                    painter1.scale(scale, scale);
                    QFont font = painter1.font() ;
                    QString str = "MENU";
                    QString str2= "EXIT";
                  
                    painter1.drawText(-2800,-330 ,str);
                     painter1.drawText(+2800,-330 ,str2);
                    painter1.drawImage(-1000 ,-280, imge);
                    painter1.end();
                  

                  }

                  My output is,

                  How to set up, I want to set up as per below image.

                  It looks like you're trying to rotate text in your QPainter code, but it's not currently working as you expect. One way to rotate text in QPainter is to use the rotate() function. This function takes an angle in degrees as its argument and rotates the painter's coordinate system by that amount. For example, if you want to rotate your text by 90 degrees, you would use the following code:

                  painter1.rotate(90);
                  painter1.drawText(-2800,-330 ,str);
                  painter1.rotate(-90);

                  You can also use the setTransform() function to set the transformation matrix for the painter, which allows you to rotate, scale, and translate the painter's coordinate system all at once.

                  Another thing you can try is use QGraphicsTextItem and set the rotate property of it.

                  QGraphicsTextItem* text = new QGraphicsTextItem(str);
                  text->setRotation(90);

                  You can also try to use QTextOption to set the rotation of text.

                  QTextOption option;
                  option.setUseDesignMetrics(true);
                  option.setTextDirection(Qt::RightToLeft);
                  option.setAlignment(Qt::AlignRight);
                  painter1.drawText(QRectF(0,0,100,100),str,option);

                  It's hard to say exactly what's causing the problem without seeing the rest of your code, but hopefully, these suggestions will help you get the rotation working as you expect.

                  1 Reply Last reply
                  1
                  • JonBJ JonB

                    @Ramkumar-Mohan
                    My last word on this, since I have never done it. After rotation, are you sure the (x, y) coordinates for drawText() don't need altering? Are you now drawing, say, outside the visible area?

                    Ramkumar MohanR Offline
                    Ramkumar MohanR Offline
                    Ramkumar Mohan
                    wrote on last edited by
                    #9

                    @JonB

                    Now Working fine ,
                    Thanks a lot....

                    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