Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QImage Rotation

    General and Desktop
    6
    13
    7727
    Loading More Posts
    • 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.
    • R
      rtavakko last edited by

      Hey guys,

      I'm trying to create a customized qdial by making my own paintevent to display and rotate a QImage:

      void Knob::paintEvent(QPaintEvent *)
      {
      QPainter painter(this);

      QImage knobPix("C:\\Users\\Roham\\Documents\\Buzz\\knob.png");
      
      QMatrix rot;
      
      qDebug() << ((double)(this->value())/(double)(this->maximum()))*270;
      
      rot.rotate((double)((double)(this->value())/(double)(this->maximum())*270));
      
      QImage out = knobPix.transformed(rot);
      
      qDebug() << QString::number(this->width()) << "," << QString::number(this->height());
      
      painter.drawImage(QPoint(0,0),out);
      

      }

      This code doesnt rotate my QImage properly, it distorts and moves it around (it doesnt rotate the image around its center). It seems to be working fine for angles that are multiples of 90 degrees. I've tried translating the image to its width/2, height/2 and back afterwards with the same results.

      Any tips or advice you guys would have will be much appreciated!!

      Thanks a bunch!

      jsulm K 2 Replies Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @rtavakko last edited by

        @rtavakko Wouldn't it be easier and probably faster to use a set of pictures each one rotated relative to previous one? Then simply paint one after another (similar to animated GIF).

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

        1 Reply Last reply Reply Quote 0
        • K
          Konstantin Tokarev @rtavakko last edited by

          @rtavakko Don't use QImage::transformed, instead set transform to your QPainter

          1 Reply Last reply Reply Quote 4
          • mrjj
            mrjj Lifetime Qt Champion last edited by

            Hi
            as @Konstantin-Tokarev suggests , just use painter
            http://www.bogotobogo.com/Qt/Qt5_QPainter_Transformation.php

            1 Reply Last reply Reply Quote 2
            • R
              rtavakko last edited by

              @mrjj @Konstantin-Tokarev @jsulm Thank you guys for your replies. I think it would be easier to transform the original image as opposed to applying a transfomration to the last rotated image.

              I applied the transformation directly to the painter and the image now rotates correctly. But the image is still being distorted. Is there something I'm missing here?

              Kent-Dorfman 1 Reply Last reply Reply Quote 0
              • R
                rtavakko last edited by rtavakko

                Hi guys, anyone have a solution to this? I've updated my paintEvent as you guys suggested and the image still distorts when it rotates. Angles that are multiples of 90 seem to be ok (0,90,180,360).

                void Knob::paintEvent(QPaintEvent *)
                {
                QPainter painter(this);

                painter.translate((double)(knobPix.width()/2),(double)(knobPix.height()/2));
                painter.rotate((double)((double)(this->value())/(double)(this->maximum())*360));
                painter.translate((double)(-knobPix.width()/2),(double)(-knobPix.height()/2));
                painter.drawImage(knobPix.rect(),knobPix);
                

                }

                1 Reply Last reply Reply Quote 0
                • Kent-Dorfman
                  Kent-Dorfman @rtavakko last edited by

                  @rtavakko said in QImage Rotation:

                  @mrjj @Konstantin-Tokarev @jsulm Thank you guys for your replies. I think it would be easier to transform the original image as opposed to applying a transfomration to the last rotated image.

                  I applied the transformation directly to the painter and the image now rotates correctly. But the image is still being distorted. Is there something I'm missing here?

                  Define "distorted". What exactly is distorted, and how?

                  R 1 Reply Last reply Reply Quote 0
                  • R
                    rtavakko @Kent-Dorfman last edited by

                    @Kent-Dorfman 0_1554239424118_Untitled.png

                    The image is pixelated when rotated. Its most obvious around the edges. In the picture all the knobs except the bottom right one (this one is rotated at 0 degrees of rotation) are distorted. You can see the saw-like edges of the other ones. Let me know if you can see it from the picture.

                    1 Reply Last reply Reply Quote 0
                    • Kent-Dorfman
                      Kent-Dorfman last edited by

                      try the QPainter::SmoothPixmapTransform render hint applied to the painter and report back..

                      R 1 Reply Last reply Reply Quote 5
                      • R
                        rtavakko @Kent-Dorfman last edited by

                        @Kent-Dorfman Yes, that fixed it. Thank you very much for your help!

                        1 Reply Last reply Reply Quote 0
                        • mgrondin
                          mgrondin last edited by

                          0_1565278071727_bad_rotation.gif

                          In my case I need to save to a file the transformed QImage. Scale (achieved by QImage::scaled) and crop (achieved by QImage::copy) works fine to produce a new QImage but the rotation result is not what I want.

                          Reading in the documentation:
                          https://doc.qt.io/qt-5/qimage.html#image-transformations
                          the transformation matrix is adjusted to compensate for unwanted translation, i.e. transformed() returns the smallest image containing all transformed points of the original image.

                          This seems to explain the behavior of the rotation. Is there a way to avoid the "compensation" mentioned above?

                          mrjj 1 Reply Last reply Reply Quote 0
                          • mrjj
                            mrjj Lifetime Qt Champion @mgrondin last edited by

                            @mgrondin
                            Hi
                            Looking pretty nice. :=)
                            I was wondering if
                            QTransform QImage::trueMatrix(const QTransform &matrix, int width, int height)
                            could be used as the docs says

                            When transforming an image using the transformed() function, the
                            transformation matrix is internally adjusted to compensate for
                            unwanted translation, i.e. transformed() returns the smallest
                            image containing all transformed points of the original image.
                            This function returns the modified matrix, which maps points
                            correctly from the original image into the new image.

                            as it sounds like what you are asking. Sorry if not. just asking.

                            mgrondin 1 Reply Last reply Reply Quote 0
                            • mgrondin
                              mgrondin @mrjj last edited by

                              Thank you @mrjj for the quick answer.

                              The QImage::trueMatrix does indeed return the modified matrix used by QImage::transformed.
                              Can the trueMatrix be used perform the inverse compensation transformation? From what I understand, the QImage::transformed function will always try to compensate thus leading in an even more distorted image.

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post