Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Learning
  3. Qt in Education
  4. Problem with setPos() and setRotation on QGraphicsItem
Qt 6.11 is out! See what's new in the release blog

Problem with setPos() and setRotation on QGraphicsItem

Scheduled Pinned Locked Moved Qt in Education
qtgraphicsitemsetpossetrotation
1 Posts 1 Posters 1.7k 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.
  • AfterworkA Offline
    AfterworkA Offline
    Afterwork
    wrote on last edited by Afterwork
    #1

    Hi everyone ! I have problem with function setPos() on QGraphicsItem . I want the rect can rotate only,rotate and move (moving on a cycle) and only move. Here is class where I use it :

    #include <QPainter>
    #include <QGraphicsItem>
    #include <QGraphicsScene>
    
    class robot : public QObject , public QGraphicsItem
    {
    public:
        robot();
        QRectF boundingRect() const;
        void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    
        void setSpeed (int);
        void setSize(int ,int );
        void setAngle(int);
        void setStep(int);
        int  CurrentAngle();
    
    protected:
        void advance(int phase);
    
    private:
        qreal a;
        QRect size;
        qreal angle,speed,stepAngle;
        void DoCollision();
    };
    

    I have the rect on QGraphicsScene and i want it rotate around it's center. I use

    robot::robot()
    {
        speed = 0;
        angle = 0;
        stepAngle = 0;
        a = 0;
        size = QRect(0,0,20,20);
        setTransformOriginPoint(10,10); // center of a rect
        setPos(mapToParent(300,300));
    }
    

    and in advance:

    void robot::advance(int phase)
    {
        if(!phase) return;
        setRotation(rotation()+stepAngle);
    }
    

    and it is working : the rect is rotating all time around it's center . But when i write function

    void robot::advance(int phase)
    {
        if(!phase) return;
        setRotation(rotation()+stepAngle);
        **setPos(mapToParent(0,-speed));**   // speed = 0 , stepAngle > 0
    }
    

    the rect is rotating and moving ( but speed = 0 ) , and its moving trajectory seems like cycloid and rect goes beyond qgraphicswiev. I notice when i write

    setTransformOriginPoint(0,0);  // not 10,10 ( not center)
    

    instead of

    setTransformOriginPoint(10,10); 
    

    it is working fine ( is rotating without moving ) even with

    void robot::advance(int phase)
    {
        if(!phase) return;
        setRotation(rotation()+stepAngle);
        **setPos(mapToParent(0,-speed));**   
    }
    

    I want rect to rotate(stepAngle != 0 ) , rotate and move ( moving on a cycle) and move ( on line,stepAngle = 0 ) with setTransformOriginPoint(rect.center()) . Thanks for help !

    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