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. QGraphicsView 90 degree Rotation
Forum Updated to NodeBB v4.3 + New Features

QGraphicsView 90 degree Rotation

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 4 Posters 11.4k 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.
  • keksi venksiK Offline
    keksi venksiK Offline
    keksi venksi
    wrote on last edited by
    #1

    Hi all ,

    I try to apply rotation through transform.rotate(90) , Rotation works fine in this case . But when i try to apply zoom , the view disappears in this case . So I wish to find some other way for rotation .

    I have found some example for rotation . Among all transform.setmatrix (m11,m12,m13,m21,m22,m23,m31,m32,m33) works fine .But In the example the matrix values are calculated for 180 degree .
    I want to find the values for 90 degree .
    Means can any one help me to find the values of the matrix for 90 degree

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi!

      See Rotation matrix, Affine transformations, Homogeneous coordinates.

      1 Reply Last reply
      3
      • keksi venksiK Offline
        keksi venksiK Offline
        keksi venksi
        wrote on last edited by keksi venksi
        #3

        Hi !!!!

        Thanks for your reply .
        There is a correction in my angle im using clokwise 90 degree So the angle is -90 degree
        I got the coordinates for the -90 degree matrix
        m11= 0 m12 = 1 m13 = 0
        m21 = -1 m22 = 0 m23 = 0
        m31 = 0 m32 = 0 m33 = 1

        I got the actual transformation which i want .
        But I got another one issue by achieving this.
        When you look into the matrix the value m11 and m22 are equal to zero .
        These m11 is for horizontal scaling and m22 is for vertical scaling
        So in wheelevent when i try to scale the value of horizontal and vertical scaling coordinates it becomes zero
        **view.scale(view.transform().m11()1.05 , view.transform().m22()1.05)
        The value becomes zero in both X and Y.
        So Now my questions i have achieved the transformation but I want some help for handling the wheel vent to zoom the view along -90 degree
        Please can anyone help me on this issue to handle the wheelevent to acheive zoomIn() and zoomOut()

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by A Former User
          #4

          See example code below.

          customgraphicsview.h

          #ifndef CUSTOMGRAPHICSVIEW_H
          #define CUSTOMGRAPHICSVIEW_H
          
          #include <QGraphicsView>
          
          class CustomGraphicsView : public QGraphicsView
          {
              Q_OBJECT
          public:
              explicit CustomGraphicsView(QWidget *parent = nullptr);
              void reset();
          
          protected:
              void wheelEvent(QWheelEvent* event) override;
          
          private:
              void updateTransform();
              double m_scale = 1.0;
              double m_angle = 0.0;
          };
          
          #endif // CUSTOMGRAPHICSVIEW_H
          

          customgraphicsview.cpp

          #include "customgraphicsview.h"
          #include <QBrush>
          #include <QColor>
          #include <QWheelEvent>
          
          CustomGraphicsView::CustomGraphicsView(QWidget *parent)
              : QGraphicsView(parent)
          {
              setBackgroundBrush(QBrush(QColor("grey")));
          }
          
          void CustomGraphicsView::reset()
          {
              m_scale = 1.0;
              m_angle = 0.0;
              updateTransform();
          }
          
          void CustomGraphicsView::wheelEvent(QWheelEvent *event)
          {
              auto const delta = (event->delta() <0.0) ? -1.0 : 1.0;
              if (event->modifiers() & Qt::ShiftModifier)
                  m_scale += 0.1 * delta;
              else
                  m_angle += 15.0 * delta;
              updateTransform();
          }
          
          void CustomGraphicsView::updateTransform()
          {
              QTransform t;
              t.scale(m_scale, m_scale);
              t.rotate(m_angle);
              setTransform(t);
          }
          
          keksi venksiK 1 Reply Last reply
          4
          • ? A Former User

            See example code below.

            customgraphicsview.h

            #ifndef CUSTOMGRAPHICSVIEW_H
            #define CUSTOMGRAPHICSVIEW_H
            
            #include <QGraphicsView>
            
            class CustomGraphicsView : public QGraphicsView
            {
                Q_OBJECT
            public:
                explicit CustomGraphicsView(QWidget *parent = nullptr);
                void reset();
            
            protected:
                void wheelEvent(QWheelEvent* event) override;
            
            private:
                void updateTransform();
                double m_scale = 1.0;
                double m_angle = 0.0;
            };
            
            #endif // CUSTOMGRAPHICSVIEW_H
            

            customgraphicsview.cpp

            #include "customgraphicsview.h"
            #include <QBrush>
            #include <QColor>
            #include <QWheelEvent>
            
            CustomGraphicsView::CustomGraphicsView(QWidget *parent)
                : QGraphicsView(parent)
            {
                setBackgroundBrush(QBrush(QColor("grey")));
            }
            
            void CustomGraphicsView::reset()
            {
                m_scale = 1.0;
                m_angle = 0.0;
                updateTransform();
            }
            
            void CustomGraphicsView::wheelEvent(QWheelEvent *event)
            {
                auto const delta = (event->delta() <0.0) ? -1.0 : 1.0;
                if (event->modifiers() & Qt::ShiftModifier)
                    m_scale += 0.1 * delta;
                else
                    m_angle += 15.0 * delta;
                updateTransform();
            }
            
            void CustomGraphicsView::updateTransform()
            {
                QTransform t;
                t.scale(m_scale, m_scale);
                t.rotate(m_angle);
                setTransform(t);
            }
            
            keksi venksiK Offline
            keksi venksiK Offline
            keksi venksi
            wrote on last edited by keksi venksi
            #5

            @Wieland said in QGraphicsView 90 degree Rotation:

            void CustomGraphicsView::updateTransform()
            {
            QTransform t;
            t.scale(m_scale, m_scale);
            t.rotate(m_angle);
            setTransform(t);
            }

            Thanks for your kind reply with full code .
            When i try to apply this code the code scales the view in x direction . In my caes if i rotate my view 90 clockwise it should scale in Y direction .

            Means in normal mode when i try to zoom , the view should expand by width mean while when i rotate and zoom in that rotated view it should expand in height

            1 Reply Last reply
            0
            • keksi venksiK Offline
              keksi venksiK Offline
              keksi venksi
              wrote on last edited by keksi venksi
              #6

              A Short note to all regarding this issue .

              Currently I have achieved the 90 degree clockwise rotation thourgh

              QTransform transform1;
              transform1.rotate(90);
              // Set the Views transformation
              view()->setTransform(transform1);
              

              Rotation works fine

              I try to achieve zooming functionality using fitInView()

              Normal case
              fitInView(0,0,calcValue,viewRect.height())

              In this case , zooming works fine

              90 rotation case
              fitInView(0,0,viewRect.weight(),calcValue)

              In this case , zooming is not not achieved . Please help me .

              I dont want to use scale() method because I set some restrictions in zooming through calcValue depending on the delta of the wheelEvent

              1 Reply Last reply
              0
              • keksi venksiK Offline
                keksi venksiK Offline
                keksi venksi
                wrote on last edited by
                #7

                Please some one look into this issue . Timely helps are really appreciable .

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  Sry, I think I don't understand exactly what you want to do. Maybe you can draw a few pictures to help my imagination :)

                  keksi venksiK 1 Reply Last reply
                  0
                  • keksi venksiK Offline
                    keksi venksiK Offline
                    keksi venksi
                    wrote on last edited by keksi venksi
                    #9

                    I tried to add image in my post but i cant .
                    Can you help me on it

                    ? 1 Reply Last reply
                    0
                    • ? A Former User

                      Sry, I think I don't understand exactly what you want to do. Maybe you can draw a few pictures to help my imagination :)

                      keksi venksiK Offline
                      keksi venksiK Offline
                      keksi venksi
                      wrote on last edited by keksi venksi
                      #10

                      @Wieland

                      X Mode

                      **-----------------------------|

                      -----------------------------**
                      Width

                      This is how I locate my graphicsview at initial stage

                      Im achieving zooming through wheel event .

                      I have currentZoomingWidth level if the delta is +ve then i will increase the currentZoomingWidth level and if it is -ve then i will decrease the currentZoomingWidth
                      So finally i find the width to expand (currentZoomingWidth )

                      wheelvent()
                      {
                      fitInVieW(0,0,currentZoomingWidth ,view().geometry().height())
                      }
                      So zooming is achieved through fitInView()

                      Y MODE
                      | |
                      | |
                      | |
                      | |
                      | | Height
                      | |
                      | |

                      Width

                      Like same case im calculating the currentZoomingWidth depending upon the delta and applying it to fitInView() height

                      wheelvent()
                      {
                      fitInVieW(0,0 ,view().geometry().width(),currentZoomingWidth)
                      }

                      In X mode i apply the calculated zoom factor to width and in Y mode I apply it to the height

                      My problem is In X mode the fit in view works fine but in Y mode it works weird

                      1 Reply Last reply
                      0
                      • keksi venksiK keksi venksi

                        I tried to add image in my post but i cant .
                        Can you help me on it

                        ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        @keksi-venksi said in QGraphicsView 90 degree Rotation:

                        I tried to add image in my post but i cant .
                        Can you help me on it

                        Hi! The image-upload feature on our forum is broken, you might see the picture but other users don't. Please upload your image to a image hoster of your choice (e.g. postimage.org) and embed the pic here with the following markup: ![alternate text](image-url). See also How to insert image on this forum and Hitchhiker's Visual Guide to the Qt Forum.

                        keksi venksiK 1 Reply Last reply
                        1
                        • ? A Former User

                          @keksi-venksi said in QGraphicsView 90 degree Rotation:

                          I tried to add image in my post but i cant .
                          Can you help me on it

                          Hi! The image-upload feature on our forum is broken, you might see the picture but other users don't. Please upload your image to a image hoster of your choice (e.g. postimage.org) and embed the pic here with the following markup: ![alternate text](image-url). See also How to insert image on this forum and Hitchhiker's Visual Guide to the Qt Forum.

                          keksi venksiK Offline
                          keksi venksiK Offline
                          keksi venksi
                          wrote on last edited by keksi venksi
                          #12

                          @Wieland

                          I have explained my query in my previous post . Please give your suggestions
                          graphicsview

                          Any how i have added the image here .This is how my graphicsview rotated .
                          Now i wanna apply zooming functionality using fitinview in this 90 degree rotated graphicsview .

                          This is how Y Mode looks

                          kshegunovK 1 Reply Last reply
                          0
                          • keksi venksiK keksi venksi

                            @Wieland

                            I have explained my query in my previous post . Please give your suggestions
                            graphicsview

                            Any how i have added the image here .This is how my graphicsview rotated .
                            Now i wanna apply zooming functionality using fitinview in this 90 degree rotated graphicsview .

                            This is how Y Mode looks

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by
                            #13

                            Use two QTransform objects - one for rotation and one for scaling, then multiply them in the proper order (the order of multiplication matters as scaling and rotation don't commute). After that apply your transform to whatever you need.

                            Read and abide by the Qt Code of Conduct

                            keksi venksiK 1 Reply Last reply
                            0
                            • kshegunovK kshegunov

                              Use two QTransform objects - one for rotation and one for scaling, then multiply them in the proper order (the order of multiplication matters as scaling and rotation don't commute). After that apply your transform to whatever you need.

                              keksi venksiK Offline
                              keksi venksiK Offline
                              keksi venksi
                              wrote on last edited by
                              #14

                              @kshegunov
                              Thanks for your reply .
                              Can you give some example code .

                              kshegunovK 1 Reply Last reply
                              0
                              • keksi venksiK keksi venksi

                                @kshegunov
                                Thanks for your reply .
                                Can you give some example code .

                                kshegunovK Offline
                                kshegunovK Offline
                                kshegunov
                                Moderators
                                wrote on last edited by
                                #15
                                QTransform rotation;
                                rotation.rotate(90);
                                
                                QTransform scaling;
                                scaling.scale(0.5, 1);
                                
                                setTransform(rotation * scaling);
                                // Or
                                QVector2D vector;
                                vector = rotation * scaling * vector;
                                

                                Modify according to your needs.

                                Read and abide by the Qt Code of Conduct

                                keksi venksiK 1 Reply Last reply
                                1
                                • kshegunovK kshegunov
                                  QTransform rotation;
                                  rotation.rotate(90);
                                  
                                  QTransform scaling;
                                  scaling.scale(0.5, 1);
                                  
                                  setTransform(rotation * scaling);
                                  // Or
                                  QVector2D vector;
                                  vector = rotation * scaling * vector;
                                  

                                  Modify according to your needs.

                                  keksi venksiK Offline
                                  keksi venksiK Offline
                                  keksi venksi
                                  wrote on last edited by keksi venksi
                                  #16

                                  @kshegunov

                                  Thanks for your reply .

                                  In my case I dont want to use scale to zoom

                                  I wish to use fitInView() to achieve zoom.

                                  In short note after applying transform (rotate 90 degree) fitInView() does not works fine . I want fitInView() to do zoom

                                  keksi venksiK 1 Reply Last reply
                                  0
                                  • keksi venksiK keksi venksi

                                    @kshegunov

                                    Thanks for your reply .

                                    In my case I dont want to use scale to zoom

                                    I wish to use fitInView() to achieve zoom.

                                    In short note after applying transform (rotate 90 degree) fitInView() does not works fine . I want fitInView() to do zoom

                                    keksi venksiK Offline
                                    keksi venksiK Offline
                                    keksi venksi
                                    wrote on last edited by keksi venksi
                                    #17

                                    @keksi-venksi

                                    Can any one help me on it .
                                    Helps are really appreciable

                                    keksi venksiK 1 Reply Last reply
                                    0
                                    • ? Offline
                                      ? Offline
                                      A Former User
                                      wrote on last edited by
                                      #18

                                      customgraphicsview.h

                                      #ifndef CUSTOMGRAPHICSVIEW_H
                                      #define CUSTOMGRAPHICSVIEW_H
                                      
                                      #include <QGraphicsView>
                                      
                                      class CustomGraphicsView : public QGraphicsView
                                      {
                                          Q_OBJECT
                                      public:
                                          enum class Mode
                                          {
                                              x,
                                              y
                                          };
                                      
                                          explicit CustomGraphicsView(QWidget *parent = nullptr);
                                          void reset();
                                          void fit();
                                          void setMode(Mode mode);
                                      
                                      protected:
                                          void wheelEvent(QWheelEvent* event) override;
                                      
                                      private:
                                          void updateTransform();
                                          Mode m_mode = Mode::x;
                                          double m_scale = 1.0;
                                      };
                                      
                                      #endif // CUSTOMGRAPHICSVIEW_H
                                      

                                      customgraphicsview.cpp

                                      #include "customgraphicsview.h"
                                      #include <QBrush>
                                      #include <QColor>
                                      #include <QWheelEvent>
                                      #include <QDebug>
                                      
                                      CustomGraphicsView::CustomGraphicsView(QWidget *parent)
                                          : QGraphicsView(parent)
                                      {
                                          setBackgroundBrush(QBrush(QColor("grey")));
                                      }
                                      
                                      void CustomGraphicsView::reset()
                                      {
                                          m_mode = Mode::x;
                                          m_scale = 1.0;
                                          updateTransform();
                                      }
                                      
                                      void CustomGraphicsView::fit()
                                      {
                                          auto const r = scene()->itemsBoundingRect();
                                          switch (m_mode) {
                                          case Mode::x: {
                                              double f1 = width() / (r.width() + 2);
                                              double f2 = height() / (r.height() + 2);
                                              m_scale = qMin(f1,f2);
                                              updateTransform();
                                              break;
                                          }
                                          case Mode::y: {
                                              double f1 = height() / (r.width() + 2);
                                              double f2 = width() / (r.height() + 2);
                                              m_scale = qMin(f1,f2);
                                              updateTransform();
                                              break;
                                          }
                                          }
                                      }
                                      
                                      void CustomGraphicsView::setMode(CustomGraphicsView::Mode mode)
                                      {
                                          if (m_mode == mode)
                                              return;
                                          m_mode = mode;
                                          updateTransform();
                                      }
                                      
                                      void CustomGraphicsView::wheelEvent(QWheelEvent *event)
                                      {
                                          auto const delta = (event->delta() <0.0) ? -1.0 : 1.0;
                                          m_scale += 0.1 * delta;
                                          updateTransform();
                                      }
                                      
                                      void CustomGraphicsView::updateTransform()
                                      {
                                          QTransform t;
                                          if (m_mode == Mode::y)
                                              t.rotate(90);
                                          t.scale(m_scale, m_scale);
                                          setTransform(t);
                                      }
                                      
                                      1 Reply Last reply
                                      1
                                      • keksi venksiK keksi venksi

                                        @keksi-venksi

                                        Can any one help me on it .
                                        Helps are really appreciable

                                        keksi venksiK Offline
                                        keksi venksiK Offline
                                        keksi venksi
                                        wrote on last edited by
                                        #19

                                        @keksi-venksi

                                        I would like to continue with this thread

                                        in the normal mode im using view->fitInView(rect.x(),0,rect.width(),30)

                                        in the 90 degree rotated mode im using view->fitInView(0,rect.y(),30,rect.height())

                                        I have tried with different combinations of fitInView for 90 degree rotataion Nothing works

                                        Can any one suggest me the coordinates for the fitInView() for the rotated view

                                        Help me on it

                                        ? 1 Reply Last reply
                                        0
                                        • keksi venksiK keksi venksi

                                          @keksi-venksi

                                          I would like to continue with this thread

                                          in the normal mode im using view->fitInView(rect.x(),0,rect.width(),30)

                                          in the 90 degree rotated mode im using view->fitInView(0,rect.y(),30,rect.height())

                                          I have tried with different combinations of fitInView for 90 degree rotataion Nothing works

                                          Can any one suggest me the coordinates for the fitInView() for the rotated view

                                          Help me on it

                                          ? Offline
                                          ? Offline
                                          A Former User
                                          wrote on last edited by
                                          #20

                                          @keksi-venksi Please try the code above that I just posted. If I understood you right, then it should meet your requirements :)

                                          keksi venksiK 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