So I'm messing around with the QGraphicsView framework. I thought to make a port of the old game Kung Fu from the NES. But I'm already having difficulties.

To begin I subclassed QGraphicsView and reimplemented the drawBackground method so that I could create my own scrolling background, updated in the widgets timerEvent(), but things are not working as expected.

Everything is fine, except the fact that the window does not repaint itself until I minimize/maximize. Its a little bizarre actually. The drawBackground method is being called regularly, where in theory I should be seeing the scrolling background changes, but I see no change until I minimize/maximize.

Here is my class and main.cpp. Hopefully someone can point me in the right direction. I'm a little lost.

Qt Code:
  1. #include <QApplication>
  2.  
  3. #include "GraphicsSceneTest.h"
  4. #include "GraphicsViewTest.h"
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. QApplication a(argc, argv);
  9.  
  10. GraphicsSceneTest graphics_scene;
  11. graphics_scene.setSceneRect(0, 0, 2000, 600);
  12.  
  13. GraphicsViewTest graphics_view(&graphics_scene);
  14. graphics_view.show();
  15.  
  16. return a.exec();
  17. }
  18.  
  19.  
  20. #ifndef GRAPHICSVIEWTEST_H
  21. #define GRAPHICSVIEWTEST_H
  22.  
  23. #include <QtGui>
  24.  
  25. class GraphicsViewTest : public QGraphicsView
  26. {
  27. Q_OBJECT
  28.  
  29. public:
  30. GraphicsViewTest(QGraphicsScene * scene, QWidget * parent = 0);
  31. virtual ~GraphicsViewTest();
  32.  
  33. protected:
  34. void drawBackground(QPainter * painter, const QRectF & rect);
  35.  
  36. void timerEvent(QTimerEvent * te);
  37.  
  38. void keyPressEvent(QKeyEvent * ke);
  39. void keyReleaseEvent(QKeyEvent * ke);
  40.  
  41. private:
  42. QPixmap m_pixmap;
  43.  
  44. QRectF m_bg_one;
  45. QRectF m_bg_two;
  46.  
  47. bool m_move_left_pressed;
  48. bool m_move_right_pressed;
  49. bool m_kick_pressed;
  50. bool m_punch_pressed;
  51. bool m_jump_pressed;
  52. bool m_crouch_pressed;
  53. };
  54.  
  55. #endif // GRAPHICSVIEWTEST_H
  56.  
  57.  
  58. #include <QtGui>
  59.  
  60. #include "GraphicsSceneTest.h"
  61. #include "GraphicsViewTest.h"
  62.  
  63. GraphicsViewTest::GraphicsViewTest(QGraphicsScene * scene, QWidget * parent) : QGraphicsView(scene, parent),
  64. m_move_left_pressed(false),
  65. m_move_right_pressed(false),
  66. m_kick_pressed(false),
  67. m_punch_pressed(false),
  68. m_jump_pressed(false),
  69. m_crouch_pressed(false)
  70. {
  71. this->setFixedSize(800, 600);
  72.  
  73. this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  74. this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  75.  
  76. this->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
  77.  
  78. m_pixmap.load("c:/kung_fu_bg.jpg");
  79.  
  80. m_bg_one.setRect(0, 0, m_pixmap.width(), m_pixmap.height());
  81. m_bg_two.setRect(m_pixmap.width(), 0, m_pixmap.width(), m_pixmap.height());
  82.  
  83. this->startTimer(1000 / 30);
  84. }
  85.  
  86. GraphicsViewTest::~GraphicsViewTest()
  87. {
  88. }
  89.  
  90. void GraphicsViewTest::drawBackground(QPainter * painter, const QRectF & rect)
  91. {
  92. painter->drawPixmap(m_bg_one, m_pixmap, QRectF(m_pixmap.rect()));
  93. painter->drawPixmap(m_bg_two, m_pixmap, QRectF(m_pixmap.rect()));
  94. }
  95.  
  96. void GraphicsViewTest::timerEvent(QTimerEvent * te)
  97. {
  98. //if button is pressed in either direction, advance scroll value
  99. if(m_move_left_pressed && !m_move_right_pressed)
  100. {
  101. m_bg_one.translate(-1.0, 0.0);
  102. m_bg_two.translate(-1.0, 0.0);
  103. }
  104. else if(m_move_right_pressed && !m_move_left_pressed)
  105. {
  106. m_bg_one.translate(1.0, 0.0);
  107. m_bg_two.translate(1.0, 0.0);
  108. }
  109.  
  110. this->repaint();
  111. }
  112.  
  113. void GraphicsViewTest::keyPressEvent(QKeyEvent * ke)
  114. {
  115. switch(ke->key())
  116. {
  117. case Qt::Key_A:
  118. m_move_left_pressed = true;
  119. break;
  120. case Qt::Key_D:
  121. m_move_right_pressed = true;
  122. break;
  123. case Qt::Key_Alt:
  124. m_punch_pressed = true;
  125. break;
  126. case Qt::Key_Control:
  127. m_kick_pressed = true;
  128. break;
  129. case Qt::Key_W:
  130. m_jump_pressed = true;
  131. break;
  132. case Qt::Key_S:
  133. m_crouch_pressed = true;
  134. break;
  135. default:
  136. break;
  137. }
  138. }
  139.  
  140. void GraphicsViewTest::keyReleaseEvent(QKeyEvent * ke)
  141. {
  142. switch(ke->key())
  143. {
  144. case Qt::Key_A:
  145. m_move_left_pressed = false;
  146. break;
  147. case Qt::Key_D:
  148. m_move_right_pressed = false;
  149. break;
  150. case Qt::Key_Alt:
  151. m_punch_pressed = true;
  152. break;
  153. case Qt::Key_Control:
  154. m_kick_pressed = true;
  155. break;
  156. case Qt::Key_W:
  157. m_jump_pressed = false;
  158. break;
  159. case Qt::Key_S:
  160. m_crouch_pressed = false;
  161. break;
  162. default:
  163. break;
  164. }
  165. }
To copy to clipboard, switch view to plain text mode