So I got some graphic items to move, but how can I make them go left to right?
Hi guys!
So here is a code snippet that will make 2 objects rotate around eachother.
But what I want to happen is basically just 1 graphic item to fly in from the left side and stop in the middle of the screen.
The problem is, how can I place the graphic item offscreen and for it to come in and how do I know the center of the screen?
is there a way I can figure out what the extreme left edge of the screen is? Right now the x and y coordinates are hardcoded in but I would assume there would be some function that could just give you the dimensions of the graphics view or scene.
I think to move the object I will need the following function:
Code:
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 );
}
But when I cilck animate the box doesn't move at all! :(
note: myRect is a new object I added that isn't in this code but I followed the same way m_rect was made. I'm setting *I THOUGHT* the x and y coordinates of the box but it doens't move.
here is the code:
Code:
#ifndef GRAPHICSVIEWDEMO_H
#define GRAPHICSVIEWDEMO_H
#include <QtGui/QWidget>
class GraphicsViewDemo
: public QWidget{
Q_OBJECT
public:
GraphicsViewDemo
(QWidget *parent
= 0);
private slots:
void animate();
private:
};
#endif // GRAPHICSVIEWDEMO_H
Code:
#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();
}
Code:
#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();
}
Thanks again!