# Thread: So I got some graphic items to move, but how can I make them go left to right?

1. Novice
Join Date
Oct 2009
Posts
47
Thanks
10

## 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:
Qt Code:
1. QPointF lastOffset;
2.
3.
4. for (int i = 0; i <= 200; ++i)
5. {
6. ellipseAnimation->setRotationAt( i/200.0, 360.0 * i/200.0 );
7. rectAnimation->setRotationAt( i/200.0, -720.0 * i/200.0 );
8. lastOffset.setX(i);
9.
10.
11. lastOffset.setY(0);
12. myRectAnimation->setPosAt(3,lastOffset);
13.
14. qreal s = i>100?(1.0-((i-100)/100.0)):(i/100.0);
15. rectAnimation->setScaleAt( i/200.0, 1.0+s, 1.0+s );
16. }
To copy to clipboard, switch view to plain text mode

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:
Qt Code:
1. #ifndef GRAPHICSVIEWDEMO_H
2. #define GRAPHICSVIEWDEMO_H
3.
4. #include <QtGui/QWidget>
5.
6.
7. class GraphicsViewDemo : public QWidget
8. {
9. Q_OBJECT
10.
11. public:
12. GraphicsViewDemo(QWidget *parent = 0);
13.
14. private slots:
15. void animate();
16.
17. private:
18. QGraphicsRectItem *m_rectangle;
19. };
20.
21. #endif // GRAPHICSVIEWDEMO_H
To copy to clipboard, switch view to plain text mode

Qt Code:
1. #include "graphicsviewdemo.h"
2.
3. #include <QVBoxLayout>
4.
5. #include <QPushButton>
6.
7. #include <QGraphicsView>
8. #include <QGraphicsScene>
9. #include <QGraphicsEllipseItem>
10. #include <QGraphicsRectItem>
11.
12. #include <QGraphicsItemAnimation>
13. #include <QTimeLine>
14.
15. GraphicsViewDemo::GraphicsViewDemo(QWidget *parent)
16. : QWidget(parent)
17. {
18. QVBoxLayout *layout = new QVBoxLayout( this );
19.
20. view->setBackgroundBrush( QPixmap( ":/images/desktop1366.png" ) );
21. QPushButton *button = new QPushButton( tr("Animate") );
22.
25.
26. connect( button, SIGNAL(clicked()), this, SLOT(animate()) );
27.
28. QGraphicsScene *scene = new QGraphicsScene( QRect( -50, -50, 100, 100 ) );
29. //QGraphicsScene *scene = new QGraphicsScene( QRect(-300, -300, 300, 300));
30. // m_ellipse = new QGraphicsEllipseItem( 25, -10, 20, 20 );
31. m_ellipse = new QGraphicsEllipseItem( 300, -300, 300, 300 );
32. m_ellipse->setPen( QPen(Qt::darkRed) );
33. m_ellipse->setBrush( Qt::red );
34.
35. //m_rectangle = new QGraphicsRectItem( -20, -20, 40, 40 );
36. m_rectangle = new QGraphicsRectItem(-300,-300,300,300);
37. m_rectangle->setPen( QPen(Qt::black) );
38. m_rectangle->setBrush( QBrush( QColor( 0, 200, 0, 200 ) ) );
39.
42.
43. view->setScene( scene );
44. }
45.
46. void GraphicsViewDemo::animate()
47. {
48. QTimeLine *timeLine = new QTimeLine(3000);
49. //QTimeLine *timeLine = new QTimeLine(10000);
50. timeLine->setFrameRange(0, 100);
51. //timeLine->setFrameRange(0,3000);
52. ellipseAnimation->setItem(m_ellipse);
53. ellipseAnimation->setTimeLine(timeLine);
54.
55. rectAnimation->setItem(m_rectangle);
56. rectAnimation->setTimeLine(timeLine);
57.
58. for (int i = 0; i <= 200; ++i)
59. {
60. ellipseAnimation->setRotationAt( i/200.0, 360.0 * i/200.0 );
61. rectAnimation->setRotationAt( i/200.0, -720.0 * i/200.0 );
62. qreal s = i>100?(1.0-((i-100)/100.0)):(i/100.0);
63. rectAnimation->setScaleAt( i/200.0, 1.0+s, 1.0+s );
64. }
65.
66. connect( timeLine, SIGNAL(finished()), timeLine, SLOT(deleteLater()) );
67. connect( timeLine, SIGNAL(finished()), ellipseAnimation, SLOT(deleteLater()) );
68. connect( timeLine, SIGNAL(finished()), rectAnimation, SLOT(deleteLater()) );
69.
70. timeLine->start();
71. }
To copy to clipboard, switch view to plain text mode
Qt Code:
1. #include <QtGui/QApplication>
2. #include "graphicsviewdemo.h"
3.
4. int main(int argc, char *argv[])
5. {
6. QApplication a(argc, argv);
7. GraphicsViewDemo w;
8. #ifdef Q_OS_WIN
9. w.show();
10. #else
11. w.showFullScreen();
12. #endif
13. return a.exec();
14. }
To copy to clipboard, switch view to plain text mode

Thanks again!
Last edited by technoViking; 6th November 2009 at 22:55.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.