setRotation() does not react so setTransformationOriginPoint()



  • 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.


  • Lifetime Qt Champion

    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



  • @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 :)



  • 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


  • Lifetime Qt Champion

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



  • @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


  • Lifetime Qt Champion

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



  • @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)


  • Lifetime Qt Champion

    @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.


Log in to reply