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. Sample code for rotating
Forum Updated to NodeBB v4.3 + New Features

Sample code for rotating

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 7.0k Views 1 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.
  • D Offline
    D Offline
    depecheSoul
    wrote on last edited by
    #1

    Hello.

    I am trying to make a simple example of rotating houses that I saw at this video: http://qt-project.org/videos/watch/advanced-qt-a-deep-dive-1-6-graphics-view-1 from 56:36.

    Can you please tell me is there any written form to that of similar code that I can look at. I tried to find any examples of that code because I learn the best when I look at some code, and in the video I cant do this.

    Thanks

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DerManu
      wrote on last edited by
      #2

      All "QGraphicsView examples":http://doc-snapshot.qt-project.org/4.8/examples-graphicsview.html might help you.
      If you're only looking for the perspective rotaton effect, see QTransform, and set a rotation that's not around the Z axis but e.g. X or Y. When QGraphicsView gets Rotations like that, it fakes a 3D effect by stretching/shearing the rotated objects.

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zakarrrr
        wrote on last edited by
        #3

        Trick at this point is to move the rect to (0, 0) and then rotate:

        @class Widget : public QWidget
        {
        Q_OBJECT
        public:
        Widget(QWidget *parent = 0) { }

        void paintEvent(QPaintEvent * event);
        

        };

        void Widget::paintEvent(QPaintEvent * event)
        {
        //rotate a rect around its middle point
        QPainter painter(this);

        QRect rect(150, 150, 200, 100);
        painter.drawRect(rect);
        painter.drawPoint(rect.center());
        

        // this is hard-coded : the middle point of the rect is now real (0, 0) of painter's default window system
        QRect rect2(-100, -50, 200, 100);
        painter.translate(250, 200);

        static qreal degree = 0;
        painter.rotate(degree++);
        painter.drawRect(rect2);
        painter.drawPoint(rect2.center());
        
        QTimer::singleShot(1, this, SLOT(update()));
        

        }@

        If a person is doing things behind you, he s clearly an ...
        Because he is an ..., he will tell any stories, which will make you think he is ok! -.-

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          zakarrrr
          wrote on last edited by
          #4

          But if you want to rotate the point by yourself (which wont be easy), you can do something like this : ( hope helps :) )

          @void Widget::paintEvent(QPaintEvent * event)
          {
          QPainter painter(this);
          static qreal degree = 0;

          QLine line(QPoint(100, 100), QPoint(199, 199));
          QPoint origin;
          origin.setX((line.p1().x() + line.p2().x())/2);
          origin.setY((line.p1().y() + line.p2().y())/2);
          
          painter.drawLine(line);
          
          // rotate point 1
          QPoint ptRotated1 = rotatePoint(degree, origin, line.p1());
          QPoint ptRotated2 = rotatePoint(degree, origin, line.p2());
          
          painter.drawLine(QLine(ptRotated1, ptRotated2));
          
          ++degree;
          
          QTimer::singleShot(1, this, SLOT(update()));
          

          }

          QPoint Widget::rotatePoint(int degree, QPoint origin, QPoint ptRotate) {

          double Pi = 3.14159265358979323846264338327950288419717;
          double rotAngle = degree * (Pi / 180.0);
          double sinAngle = qSin(rotAngle);
          double cosAngle = qCos(rotAngle);
          
          int px = ptRotate.x() - origin.x();
          int py = ptRotate.y() - origin.y();
          
          float xnew = px * cosAngle + py * sinAngle;
          float ynew = py * cosAngle - px * sinAngle;
          
          px = xnew + origin.x();
          py = ynew + origin.y();
          
          return QPoint(px, py);
          

          }@

          If a person is doing things behind you, he s clearly an ...
          Because he is an ..., he will tell any stories, which will make you think he is ok! -.-

          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