for (int i = 0; i <= 200; ++i)
{
ellipseAnimation->setRotationAt( i/200.0, 360.0 * i/200.0 );
rectAnimation->setRotationAt( i/200.0, -720.0 * i/200.0 );
lastOffset.setX(i);
lastOffset.setY(0);
myRectAnimation->setPosAt(3,lastOffset);
qreal s = i>100?(1.0-((i-100)/100.0)):(i/100.0);
rectAnimation->setScaleAt( i/200.0, 1.0+s, 1.0+s );
}
QPointF lastOffset;
for (int i = 0; i <= 200; ++i)
{
ellipseAnimation->setRotationAt( i/200.0, 360.0 * i/200.0 );
rectAnimation->setRotationAt( i/200.0, -720.0 * i/200.0 );
lastOffset.setX(i);
lastOffset.setY(0);
myRectAnimation->setPosAt(3,lastOffset);
qreal s = i>100?(1.0-((i-100)/100.0)):(i/100.0);
rectAnimation->setScaleAt( i/200.0, 1.0+s, 1.0+s );
}
To copy to clipboard, switch view to plain text mode
#include "graphicsviewdemo.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsEllipseItem>
#include <QGraphicsRectItem>
#include <QGraphicsItemAnimation>
#include <QTimeLine>
GraphicsViewDemo
::GraphicsViewDemo(QWidget *parent
){
view
->setBackgroundBrush
( QPixmap( ":/images/desktop1366.png" ) );
layout->addWidget( view );
layout->addWidget( button );
connect( button, SIGNAL(clicked()), this, SLOT(animate()) );
//QGraphicsScene *scene = new QGraphicsScene( QRect(-300, -300, 300, 300));
// m_ellipse = new QGraphicsEllipseItem( 25, -10, 20, 20 );
m_ellipse
->setPen
( QPen(Qt
::darkRed) );
m_ellipse->setBrush( Qt::red );
//m_rectangle = new QGraphicsRectItem( -20, -20, 40, 40 );
m_rectangle
->setPen
( QPen(Qt
::black) );
m_rectangle
->setBrush
( QBrush( QColor( 0,
200,
0,
200 ) ) );
scene->addItem( m_ellipse );
scene->addItem( m_rectangle );
view->setScene( scene );
}
void GraphicsViewDemo::animate()
{
//QTimeLine *timeLine = new QTimeLine(10000);
timeLine->setFrameRange(0, 100);
//timeLine->setFrameRange(0,3000);
ellipseAnimation->setItem(m_ellipse);
ellipseAnimation->setTimeLine(timeLine);
rectAnimation->setItem(m_rectangle);
rectAnimation->setTimeLine(timeLine);
for (int i = 0; i <= 200; ++i)
{
ellipseAnimation->setRotationAt( i/200.0, 360.0 * i/200.0 );
rectAnimation->setRotationAt( i/200.0, -720.0 * i/200.0 );
qreal s = i>100?(1.0-((i-100)/100.0)):(i/100.0);
rectAnimation->setScaleAt( i/200.0, 1.0+s, 1.0+s );
}
connect( timeLine, SIGNAL(finished()), timeLine, SLOT(deleteLater()) );
connect( timeLine, SIGNAL(finished()), ellipseAnimation, SLOT(deleteLater()) );
connect( timeLine, SIGNAL(finished()), rectAnimation, SLOT(deleteLater()) );
timeLine->start();
}
#include "graphicsviewdemo.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsEllipseItem>
#include <QGraphicsRectItem>
#include <QGraphicsItemAnimation>
#include <QTimeLine>
GraphicsViewDemo::GraphicsViewDemo(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout( this );
QGraphicsView *view = new QGraphicsView();
view->setBackgroundBrush( QPixmap( ":/images/desktop1366.png" ) );
QPushButton *button = new QPushButton( tr("Animate") );
layout->addWidget( view );
layout->addWidget( button );
connect( button, SIGNAL(clicked()), this, SLOT(animate()) );
QGraphicsScene *scene = new QGraphicsScene( QRect( -50, -50, 100, 100 ) );
//QGraphicsScene *scene = new QGraphicsScene( QRect(-300, -300, 300, 300));
// m_ellipse = new QGraphicsEllipseItem( 25, -10, 20, 20 );
m_ellipse = new QGraphicsEllipseItem( 300, -300, 300, 300 );
m_ellipse->setPen( QPen(Qt::darkRed) );
m_ellipse->setBrush( Qt::red );
//m_rectangle = new QGraphicsRectItem( -20, -20, 40, 40 );
m_rectangle = new QGraphicsRectItem(-300,-300,300,300);
m_rectangle->setPen( QPen(Qt::black) );
m_rectangle->setBrush( QBrush( QColor( 0, 200, 0, 200 ) ) );
scene->addItem( m_ellipse );
scene->addItem( m_rectangle );
view->setScene( scene );
}
void GraphicsViewDemo::animate()
{
QTimeLine *timeLine = new QTimeLine(3000);
//QTimeLine *timeLine = new QTimeLine(10000);
timeLine->setFrameRange(0, 100);
//timeLine->setFrameRange(0,3000);
QGraphicsItemAnimation *ellipseAnimation = new QGraphicsItemAnimation();
ellipseAnimation->setItem(m_ellipse);
ellipseAnimation->setTimeLine(timeLine);
QGraphicsItemAnimation *rectAnimation = new QGraphicsItemAnimation();
rectAnimation->setItem(m_rectangle);
rectAnimation->setTimeLine(timeLine);
for (int i = 0; i <= 200; ++i)
{
ellipseAnimation->setRotationAt( i/200.0, 360.0 * i/200.0 );
rectAnimation->setRotationAt( i/200.0, -720.0 * i/200.0 );
qreal s = i>100?(1.0-((i-100)/100.0)):(i/100.0);
rectAnimation->setScaleAt( i/200.0, 1.0+s, 1.0+s );
}
connect( timeLine, SIGNAL(finished()), timeLine, SLOT(deleteLater()) );
connect( timeLine, SIGNAL(finished()), ellipseAnimation, SLOT(deleteLater()) );
connect( timeLine, SIGNAL(finished()), rectAnimation, SLOT(deleteLater()) );
timeLine->start();
}
To copy to clipboard, switch view to plain text mode
#include <QtGui/QApplication>
#include "graphicsviewdemo.h"
int main(int argc, char *argv[])
{
GraphicsViewDemo w;
#ifdef Q_OS_WIN
w.show();
#else
w.showFullScreen();
#endif
return a.exec();
}
#include <QtGui/QApplication>
#include "graphicsviewdemo.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GraphicsViewDemo w;
#ifdef Q_OS_WIN
w.show();
#else
w.showFullScreen();
#endif
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks