Important: Please read the Qt Code of Conduct -

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.


    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QGraphicsPixmapItem>
    #include <QGraphicsScene>
    #include <QTimer>
    #include <QDebug>
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        scene = new QGraphicsScene();
        Robo1_Bild = QPixmap(":/Tryout/Robo_1.png");        
        Robo1 = new QGraphicsPixmapItem();
        qreal xc = Robo1->pixmap().size().height()*0.02;    // 0.02 is half the scaling factor
        qreal yc = Robo1->pixmap().size().width()*0.02;
        timer = new QTimer(this);
        angle = 1;
        connect(timer, SIGNAL(timeout()),this,SLOT(UpdateWin()));
        delete ui;
    void MainWindow::UpdateWin()


    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QGraphicsItem>
    namespace Ui {
    class MainWindow;
    class MainWindow : public QMainWindow
        explicit MainWindow(QWidget *parent = nullptr);
        QPixmap Robo1_Bild;
        QGraphicsPixmapItem *Robo1;
        qreal                angle;
    private slots:
        void UpdateWin();
        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
    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);
        const int si = 100;
        Robo1_Bild = QPixmap(si, si);
        Robo1 = new QGraphicsPixmapItem();
        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);

    it seems to do as we expect

    alt text

  • @born4pizzas


    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

    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

    @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

    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.

  • Lifetime Qt Champion

    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

    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