some parts of Qrect missing

155 views Asked by At

I'm trying to move a circle(my class inherits QGraphicsItem) by using QTimer in keyPressEvent function. here is my class:

class Shape :public QGraphicsObject
{
    Q_OBJECT
public:
   Shape();
   QRectF boundingRect() const;
   void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
   void keyPressEvent(QKeyEvent *event);
}

and the boundingRect and paintfunction:

QRectF Shape::boundingRect() const
{
    return QRectF(0,0,30,30);
}

void Shape::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{

   QRectF circle=boundingRect();
    //painter->setClipRect( option->exposedRect );
    QBrush brush(QColor(Qt::darkGreen));
    painter->setPen(QColor(Qt::darkGreen));
    painter->setBrush(brush);
    painter->drawEllipse(circle);
}

and this is how the timer works:

if(event->key()==Qt::Key_1){
        if(!start){
            timer->stop();
            timer=NULL;
        }
        setScale(1);
        timer=new QTimer(scene());
        connect(timer,SIGNAL(timeout()),this,SLOT(moveRightLeft()));
        timer->start(2);
        start=false;
}

void Shape::moveRightLeft()
{
    if(scene()->collidingItems(this).isEmpty()==true){//no collision
        setPos(x()+dir,y());
    }
    else{
        dir=-(dir);
        setPos(x()+dir,y());
    }
}

The problem is when the key is pressed after a few seconds some parts of the circle is missing depending on the direction its moving.Even prepareGeometryChange() and update() didn't work.this is the shape of the circle while moving thanks for your help!

0

There are 0 answers