ui->graphicsView->setFixedSize(500, 300);scene = new QGraphicsScene;ui->graphicsView->setScene(scene);QGraphicsLineItem *bottomline = new QGraphicsLineItem();bottomline = scene->addLine(-249.0, 148.0, 249.0, 148.0);ball = new QGraphicsEllipseItem(-10, -10, 10, 10);scene->addItem(ball);qreal dx=0;qreal dy=0;int velx=1;int vely=1;QTimeLine *timer = new QTimeLine(5000);timer->setFrameRange(0, 100);QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;animation->setItem(ball);animation->setTimeLine(timer);timer->setUpdateInterval(10);for (int i = 0; i < 400; ++i){ if(ball->collidesWithItem(bottomline, Qt::IntersectsItemShape)) vely*=-1; animation->setPosAt(i / 400.0, QPointF(dx+velx, dy+vely)); dx+=velx; dy+=vely;}timer->start();
....какой-то класс{QGraphicsScene *gs;QGraphicsLineItem *gli;QGraphicsEllipseItem *gei;в GUI имеем QGraphicsView *gv.protected: void timerEvent(QTimerEvent *);}
В конструкторе класса:gs = new QGraphicsScene(this);gs->setSceneRect(0,0,450,550);ui->gv->setFixedSize(460,560);ui->gv->setScene( gs );gli = new QGraphicsLineItem(0,500,400,500);gs->addItem( gli );gei = new QGraphicsEllipseItem(0,0,50,50);gei->setPos(200,200);gs->addItem(gei);startTimer( 100 );......void класс::timerEvent(QTimerEvent *te){ Q_UNUSED(te); gei->setPos(gei->scenePos().x(),gei->scenePos().y()+10); if( gei->collidesWithItem(gli)) { ... }}