Problem with setPos() and setRotation on QGraphicsItem



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.