0

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!

  • The only thing I see is that the pen width of 1 will be drawing outside of the object's bounding rectangle. A width of 1 will actually be averaged over two scene coordinates. Even so, I would expect that to cause drawing artifacts as it moved, not the truncation you see. Still, try setting the pen as "setPen (Qt::NoPen)". Since the line and brush are the same solid color, it won't make a significant visual difference. You shouldn't need to call "update" or "prepareGeometryChange" when just setting the position, so it's fine without them. – goug Dec 22 '16 at 19:05
  • @goug thanks for your comment but setting no pen didn't worked :( – niloofarshahbaz Dec 22 '16 at 20:52
  • Is there any possibility that something else that's white is drawing over the top of the circle? Also, just a long shot, but maybe put a "prepareGeometryChange" call in your object's constructor? I'm not accustomed to a hard-coded bounding rectangle, and I'm wondering if that's confusing the scene somehow. – goug Dec 22 '16 at 21:08
  • @goug I don't know what is wrong with this..there's only a circle and 4 lines creating a rectangle...for the first few seconds it works perfectly and if I set the timer to be repeated every 6 or higher seconds ,this truncation doesn't happen...and prepareGeometryChange didn't work. – niloofarshahbaz Dec 23 '16 at 08:18
  • of course I think prepareGeometryChange() in the constructor made it happen less than before – niloofarshahbaz Dec 23 '16 at 08:29
  • I can't reproduce the movement, so i apparently don't have enough code from the above. However, did you call the base class constructor from your constructor: Shape::Shape () : QGraphicsObject {...} – goug Dec 27 '16 at 20:02

0 Answers0