Qrect的某些部分缺失

时间:2016-12-22 16:12:29

标签: c++ qt qtgui

我试图通过在keyPressEvent函数中使用QTimer来移动一个圆圈(我的类继承了QGraphicsItem)。 这是我的班级:

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

和boundingRect和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);
}

这就是计时器的工作方式:

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());
    }
}

问题是当几秒钟后按下键时,圆圈的某些部分会丢失,具体取决于它的移动方向。即使prepareGeometryChange()和update()也不起作用。this is the shape of the circle while moving 谢谢你的帮助!

0 个答案:

没有答案
相关问题