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. setRotation() does not react so setTransformationOriginPoint()

setRotation() does not react so setTransformationOriginPoint()

Scheduled Pinned Locked Moved Solved General and Desktop
rotatepixmapcenter
9 Posts 3 Posters 1.8k Views
  • 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.
  • B Offline
    B Offline
    Born4Pizzas
    wrote on 31 Jul 2019, 08:53 last edited by Born4Pizzas
    #1

    Hello everybody,
    first of all thank you to the whole community here. Because I'm pretty new to Qt/ (C++ in general) I appreciate all the comprehensible explanations for common beginner-problems on here.
    Unfortunately I struggle with a pretty simple problem since days and can't work around it. I'm trying to rotate a QGraphicsPixmapItem around it's center, but although I'm redefining the origin point to the center, the function setRotation() still rotates around the point (0,0). What's even more weird is, that unlike setRotation() the setPos() function accepts the new origin point. In the following code I've stripped down the problem to the bare minimum and hope someone might find my mistake.

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QGraphicsPixmapItem>
    #include <QGraphicsScene>
    #include <QTimer>
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        scene = new QGraphicsScene();
        scene->setSceneRect(0,0,700,800);
        ui->Canvas->setScene(scene);
    
        Robo1_Bild = QPixmap(":/Tryout/Robo_1.png");        
        Robo1 = new QGraphicsPixmapItem();
        Robo1->setPixmap(Robo1_Bild);
        qreal xc = Robo1->pixmap().size().height()*0.02;    // 0.02 is half the scaling factor
        qreal yc = Robo1->pixmap().size().width()*0.02;
        Robo1->setTransformOriginPoint(-xc,-yc);
        Robo1->setPos(300,400);
        Robo1->setScale(0.04);
        scene->addItem(Robo1);
    
        //Timer
        timer = new QTimer(this);
        angle = 1;
        connect(timer, SIGNAL(timeout()),this,SLOT(UpdateWin()));
        timer->start(100);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::UpdateWin()
    {
        angle++;
        Robo1->setRotation(angle);
    }
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QGraphicsItem>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
        QPixmap Robo1_Bild;
        QGraphicsPixmapItem *Robo1;
        qreal                angle;
    
    private slots:
        void UpdateWin();
    
    private:
        Ui::MainWindow *ui;
        QGraphicsScene *scene;
        QTimer         *timer;
    };
    
    #endif // MAINWINDOW_H
    

    Thanks for any answers in advance and best regards.

    P 1 Reply Last reply 31 Jul 2019, 09:23
    1
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 31 Jul 2019, 09:20 last edited by mrjj
      #2

      Hi and welcome to the forums.

      I just think your values for
      Robo1->setTransformOriginPoint(-xc,-yc);
      is incorrect. Did not have your image(":/Tryout/Robo_1.png") so could not see what.

      if i do

          scene = new QGraphicsScene();
          scene->setSceneRect(0, 0, 700, 800);
          ui->Canvas->setScene(scene);
      
          const int si = 100;
          Robo1_Bild = QPixmap(si, si);
          Robo1_Bild.fill(Qt::red);
      
          Robo1 = new QGraphicsPixmapItem();
          Robo1->setPixmap(Robo1_Bild);
          qreal xc = Robo1->pixmap().size().height() * 0.02;  // 0.02 is half the scaling factor
          qreal yc = Robo1->pixmap().size().width() * 0.02;
          Robo1->setTransformOriginPoint(si / 2, si / 2);
          Robo1->setPos(300, 400);
          //Robo1->setScale(0.4);
          scene->addItem(Robo1);
      
      

      it seems to do as we expect

      alt text

      B 1 Reply Last reply 31 Jul 2019, 10:14
      5
      • B Born4Pizzas
        31 Jul 2019, 08:53

        Hello everybody,
        first of all thank you to the whole community here. Because I'm pretty new to Qt/ (C++ in general) I appreciate all the comprehensible explanations for common beginner-problems on here.
        Unfortunately I struggle with a pretty simple problem since days and can't work around it. I'm trying to rotate a QGraphicsPixmapItem around it's center, but although I'm redefining the origin point to the center, the function setRotation() still rotates around the point (0,0). What's even more weird is, that unlike setRotation() the setPos() function accepts the new origin point. In the following code I've stripped down the problem to the bare minimum and hope someone might find my mistake.

        mainwindow.cpp

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QGraphicsPixmapItem>
        #include <QGraphicsScene>
        #include <QTimer>
        #include <QDebug>
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            scene = new QGraphicsScene();
            scene->setSceneRect(0,0,700,800);
            ui->Canvas->setScene(scene);
        
            Robo1_Bild = QPixmap(":/Tryout/Robo_1.png");        
            Robo1 = new QGraphicsPixmapItem();
            Robo1->setPixmap(Robo1_Bild);
            qreal xc = Robo1->pixmap().size().height()*0.02;    // 0.02 is half the scaling factor
            qreal yc = Robo1->pixmap().size().width()*0.02;
            Robo1->setTransformOriginPoint(-xc,-yc);
            Robo1->setPos(300,400);
            Robo1->setScale(0.04);
            scene->addItem(Robo1);
        
            //Timer
            timer = new QTimer(this);
            angle = 1;
            connect(timer, SIGNAL(timeout()),this,SLOT(UpdateWin()));
            timer->start(100);
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        void MainWindow::UpdateWin()
        {
            angle++;
            Robo1->setRotation(angle);
        }
        

        mainwindow.h

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        #include <QGraphicsItem>
        
        namespace Ui {
        class MainWindow;
        }
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            explicit MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
        
            QPixmap Robo1_Bild;
            QGraphicsPixmapItem *Robo1;
            qreal                angle;
        
        private slots:
            void UpdateWin();
        
        private:
            Ui::MainWindow *ui;
            QGraphicsScene *scene;
            QTimer         *timer;
        };
        
        #endif // MAINWINDOW_H
        

        Thanks for any answers in advance and best regards.

        P Offline
        P Offline
        Pl45m4
        wrote on 31 Jul 2019, 09:23 last edited by Pl45m4
        #3

        @born4pizzas

        Hi,

        have you checked these values?

        qreal xc = Robo1->pixmap().size().height()*0.02; 
        qreal yc = Robo1->pixmap().size().width()*0.02;
        Robo1->setTransformOriginPoint(-xc,-yc); // this will move the origin further to the top-left
        
        

        https://doc.qt.io/archives/qt-4.8/coordsys.html
        https://doc.qt.io/archives/qt-4.8/graphicsview.html#the-graphics-view-coordinate-system

        Item Coordinates Items live in their own local coordinate system. Their coordinates are usually centered around its center point (0, 0), and this is also the center for all transformations.

        According to this, the center point is (0, 0), so your redefinition of the anchor point for your rotation will set it somewhere else. So check the actual point

        Edit:
        @mrjj was faster :)


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        2
        • M mrjj
          31 Jul 2019, 09:20

          Hi and welcome to the forums.

          I just think your values for
          Robo1->setTransformOriginPoint(-xc,-yc);
          is incorrect. Did not have your image(":/Tryout/Robo_1.png") so could not see what.

          if i do

              scene = new QGraphicsScene();
              scene->setSceneRect(0, 0, 700, 800);
              ui->Canvas->setScene(scene);
          
              const int si = 100;
              Robo1_Bild = QPixmap(si, si);
              Robo1_Bild.fill(Qt::red);
          
              Robo1 = new QGraphicsPixmapItem();
              Robo1->setPixmap(Robo1_Bild);
              qreal xc = Robo1->pixmap().size().height() * 0.02;  // 0.02 is half the scaling factor
              qreal yc = Robo1->pixmap().size().width() * 0.02;
              Robo1->setTransformOriginPoint(si / 2, si / 2);
              Robo1->setPos(300, 400);
              //Robo1->setScale(0.4);
              scene->addItem(Robo1);
          
          

          it seems to do as we expect

          alt text

          B Offline
          B Offline
          Born4Pizzas
          wrote on 31 Jul 2019, 10:14 last edited by Born4Pizzas
          #4

          Hello @mrjj, thanks a lot for the quick reply. Somehow the scaling of my picture was messing up the redefiniion of my origin point (and also redefining the origin point in the wrong directions). I was scaling a 2500x2500px picture down to 100x100px, which somehow must have messed up my values "xc" and "yc". After rescaling the source picture externaly it worked perfectly (even if I still scale the picture down internally from 200x200 to 100x100. A scaling factor 4% apperently was to much though).
          Again, thanks a lot for your code example, it helped a lot. Also thanks to @Pl45m4
          Best regards

          1 Reply Last reply
          4
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 31 Jul 2019, 10:16 last edited by
            #5

            Hi
            execellent.
            Looks interesting. Some sort of robot game?

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Born4Pizzas
              wrote on 31 Jul 2019, 10:47 last edited by
              #6

              @mrjj
              It's actually the front-end visulizitation of a simulated "real" autonomous robot-factory, transporting RFID-chips to different scanners simulating the production. This is what the final project of the class from the previous semester looked like. https://www.youtube.com/watch?v=X2C8Ajh29ak

              1 Reply Last reply
              2
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 31 Jul 2019, 10:51 last edited by
                #7

                Hi
                That is super cool.
                Does the simulator get real positioning feedback or is it purely simulated ?

                1 Reply Last reply
                2
                • B Offline
                  B Offline
                  Born4Pizzas
                  wrote on 31 Jul 2019, 11:13 last edited by
                  #8

                  @mrjj
                  We do have a camera system installed on the ceiling which is tracking the markers on the back of the robots, but we also keep track of the driven distances by measuring the wheel rotations. Those to datasets are somehow combined (no clue how the other group is doing that) and given to us as positioning feedback for visulization purpose.
                  If my group also has the spare time to make a video I'll post on here :) (Due is around december though)

                  M 1 Reply Last reply 31 Jul 2019, 11:31
                  1
                  • B Born4Pizzas
                    31 Jul 2019, 11:13

                    @mrjj
                    We do have a camera system installed on the ceiling which is tracking the markers on the back of the robots, but we also keep track of the driven distances by measuring the wheel rotations. Those to datasets are somehow combined (no clue how the other group is doing that) and given to us as positioning feedback for visulization purpose.
                    If my group also has the spare time to make a video I'll post on here :) (Due is around december though)

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 31 Jul 2019, 11:31 last edited by
                    #9

                    @born4pizzas
                    Hi
                    ok. that is really nice projects. and interesting.
                    Would love to see videos if time.
                    Happy programming then and good luck with project.

                    1 Reply Last reply
                    1

                    3/9

                    31 Jul 2019, 09:23

                    topic:navigator.unread, 6
                    • Login

                    • Login or register to search.
                    3 out of 9
                    • First post
                      3/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved