Just got it to work!!!

Apparently I had to stick something like...

Qt Code:
  1. QTimer timer;
  2. QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance()));
  3. timer.start(100);
To copy to clipboard, switch view to plain text mode 

in the main function to get it to operate correctly. If my intuition is correct, timeout is called when the timer counts down from 100 to 0 and it then calls advance(), and then the timer starts again at 100 and repeats the cycle. So, it seems to be a way for everything to synchronize.

Now, it's time for some real development to begin.

Qt Code:
  1. #include <QApplication>
  2. #include <QGraphicsEllipseItem>
  3. #include <QGraphicsScene>
  4. #include <QGraphicsView>
  5.  
  6. #include <qdatetime.h>
  7. #include <qmainwindow.h>
  8. #include <qstatusbar.h>
  9. #include <qmessagebox.h>
  10. #include <qmenubar.h>
  11. #include <qapplication.h>
  12. #include <qpainter.h>
  13. #include <qprinter.h>
  14. #include <qlabel.h>
  15. #include <qimage.h>
  16. #include <qpixmap.h>
  17. #include <QMouseEvent>
  18. #include <QStyleOptionGraphicsItem>
  19. #include <qdebug.h>
  20. #include <stdlib.h>
  21. #include <qtimer.h>
  22.  
  23. QString myImgName = "myimg.png";
  24. static QImage *myImg;
  25.  
  26.  
  27. static const int imageRTTI = 984376;
  28.  
  29.  
  30. class ImageItem: public QGraphicsRectItem
  31. {
  32. public:
  33. ImageItem( QImage img );
  34. int rtti () const { return imageRTTI; }
  35. void advance(int phase);
  36. protected:
  37. void paint( QPainter *, const QStyleOptionGraphicsItem *option, QWidget *widget );
  38. private:
  39. QImage image;
  40. QPixmap pixmap;
  41. int state;
  42. };
  43.  
  44.  
  45. void ImageItem::advance(int phase)
  46. {
  47. moveBy(0,1);
  48. }
  49.  
  50.  
  51. ImageItem::ImageItem( QImage img )
  52. : image(img)
  53. {
  54. state = 0;
  55. setRect(0, 0, image.width(), image.height());
  56. setFlag(ItemIsMovable);
  57. #if !defined(Q_WS_QWS)
  58. pixmap = pixmap.fromImage(image, Qt::OrderedAlphaDither);
  59. #endif
  60. }
  61.  
  62. void ImageItem::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget * )
  63. {
  64. // On Qt/Embedded, we can paint a QImage as fast as a QPixmap,
  65. // but on other platforms, we need to use a QPixmap.
  66. #if defined(Q_WS_QWS)
  67. p->drawImage( option->exposedRect, image, option->exposedRect, Qt::OrderedAlphaDither );
  68. #else
  69. p->drawPixmap( option->exposedRect, pixmap, option->exposedRect );
  70. #endif
  71. }
  72.  
  73. int main( int argc, char **argv )
  74. {
  75.  
  76. QApplication app(argc, argv);
  77.  
  78. scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );
  79.  
  80. myImg = new QImage;
  81. myImg->load(myImgName);
  82.  
  83.  
  84. QAbstractGraphicsShapeItem* i = new ImageItem(*myImg);
  85. scene.addItem(i);
  86. i->setPos(-50,-50);
  87.  
  88. QTimer timer;
  89. QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance()));
  90. timer.start(100);
  91.  
  92. QGraphicsView view( &scene );
  93. view.setRenderHints( QPainter::Antialiasing );
  94. view.show();
  95.  
  96. return app.exec();
  97. }
To copy to clipboard, switch view to plain text mode