Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Rotate an QLabel
Qt 6.12 LTS is out! See what's new in the release blog

Rotate an QLabel

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 20.3k Views 3 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.
  • C CptN3m0

    Hello,
    Im trying to rotate my QLabel in a timer with this code

    QPainter p(ui->label_19);
    p.rotate(90);
    p.end();
    

    all what I got are this errors:

    QWidget::paintEngine: Should no longer be called
    QPainter::begin: Paint device returned engine == 0, type: 1
    QPainter::rotate: Painter not active
    QPainter::end: Painter not active, aborted
    

    how can I fix them and let my qlabel rotate.

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @CptN3m0
    You can only use QPainter in a paintEvent()
    http://doc.qt.io/qt-5/qpainter.html

    void SimpleExampleWidget::paintEvent(QPaintEvent *)
    {
        QPainter painter(this);
        painter.setPen(Qt::blue);
        painter.setFont(QFont("Arial", 30));
        painter.drawText(rect(), Qt::AlignCenter, "Qt");
    }
    

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    C 1 Reply Last reply
    3
    • jsulmJ jsulm

      @CptN3m0
      You can only use QPainter in a paintEvent()
      http://doc.qt.io/qt-5/qpainter.html

      void SimpleExampleWidget::paintEvent(QPaintEvent *)
      {
          QPainter painter(this);
          painter.setPen(Qt::blue);
          painter.setFont(QFont("Arial", 30));
          painter.drawText(rect(), Qt::AlignCenter, "Qt");
      }
      
      C Offline
      C Offline
      CptN3m0
      wrote on last edited by
      #3

      @jsulm is there any other way to rotate QLabel? Or just an image inside QLabel.

      mrjjM 1 Reply Last reply
      0
      • C CptN3m0

        @jsulm is there any other way to rotate QLabel? Or just an image inside QLabel.

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @CptN3m0
        Hi
        Yes, you can paint to a pixmap and set that to QLabel.

        QPixmap pix(500,500);
        QPainter paint (&pix);
        .. rotate and draw text..
        label->addPixmap(pix);
        
        1 Reply Last reply
        3
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #5

          Hi,

          Or you can use QImage::transformed passing it a rotation matrix.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          5
          • C Offline
            C Offline
            CptN3m0
            wrote on last edited by
            #6

            Okey I found a solution for me

            QPixmap pixmap(*ui->label->pixmap());
            QMatrix rm;
            rm.rotate(50);
            pixmap = pixmap.transformed(rm);
            ui->label->setPixmap(pixmap);
            

            but the problem ist now, Im trying to draw the pixmap every time at the different rotate which was saved at mine vector. what is happening, it´s just rotate further

            mrjjM 1 Reply Last reply
            0
            • C CptN3m0

              Okey I found a solution for me

              QPixmap pixmap(*ui->label->pixmap());
              QMatrix rm;
              rm.rotate(50);
              pixmap = pixmap.transformed(rm);
              ui->label->setPixmap(pixmap);
              

              but the problem ist now, Im trying to draw the pixmap every time at the different rotate which was saved at mine vector. what is happening, it´s just rotate further

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @CptN3m0
              Well the normal solution is to keep the original unrotated bitmap and
              assign a copy to the label. Then when new rotate is need , the original image is used
              and not the already rotated version.

              You can also rotate it back, and rotate to a new angle but that often
              distorts the image slowly.

              C 1 Reply Last reply
              3
              • mrjjM mrjj

                @CptN3m0
                Well the normal solution is to keep the original unrotated bitmap and
                assign a copy to the label. Then when new rotate is need , the original image is used
                and not the already rotated version.

                You can also rotate it back, and rotate to a new angle but that often
                distorts the image slowly.

                C Offline
                C Offline
                CptN3m0
                wrote on last edited by
                #8

                @mrjj can u write an example?

                mrjjM 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Hi,

                  Where does your QPixmap come from in the first place ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • C CptN3m0

                    @mrjj can u write an example?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @CptN3m0
                    Wait i just realized. you are just doing it to rotate text right ?
                    Not the actual image ?
                    So only the text matters. so u can just clear it and redraw text.

                    QPixmap pixmap(*ui->label->pixmap());
                    pixmap.fill( Qr::red ); // just clear old
                    QMatrix rm;
                    rm.rotate(50);
                    pixmap = pixmap.transformed(rm);
                    ...drawtext..
                    ui->label->setPixmap(pixmap);

                    C 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @CptN3m0
                      Wait i just realized. you are just doing it to rotate text right ?
                      Not the actual image ?
                      So only the text matters. so u can just clear it and redraw text.

                      QPixmap pixmap(*ui->label->pixmap());
                      pixmap.fill( Qr::red ); // just clear old
                      QMatrix rm;
                      rm.rotate(50);
                      pixmap = pixmap.transformed(rm);
                      ...drawtext..
                      ui->label->setPixmap(pixmap);

                      C Offline
                      C Offline
                      CptN3m0
                      wrote on last edited by
                      #11

                      @mrjj 0_1524812883275_Download (1).png seems like it is a text, instead .png file

                      mrjjM 1 Reply Last reply
                      0
                      • C CptN3m0

                        @mrjj 0_1524812883275_Download (1).png seems like it is a text, instead .png file

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @CptN3m0
                        So you have 2.png in a resource file ?
                        alo, the goal is to rotate text, so the image has nothing u want to show. Just used to draw the text on ?

                        C 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @CptN3m0
                          So you have 2.png in a resource file ?
                          alo, the goal is to rotate text, so the image has nothing u want to show. Just used to draw the text on ?

                          C Offline
                          C Offline
                          CptN3m0
                          wrote on last edited by
                          #13

                          @mrjj I need to rotate this 2.png inside my QLabel. There is no thext inside

                          mrjjM 1 Reply Last reply
                          0
                          • C CptN3m0

                            @mrjj I need to rotate this 2.png inside my QLabel. There is no thext inside

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            @CptN3m0
                            Ok, so the the goal is to rotate 2.png ? and not text.
                            Anyhow, just add a QPixmap to .h and save the
                            2.png to it.
                            Then when you rotate always use the copy and not the already rotated one.

                            1 Reply Last reply
                            2

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt
                            • Unsolved