title: Qt: how to make QGraphicsPixmapItem automatically move in a repeating route
body: <p>I am making a game now, and I want my QGraphicsPixmapItem automatically move from left to right and right to left between my view repeatedly in the same route. </p>
  
  <p>To be more specific:</p>
  
  <blockquote>
    <p>my enemy start at (0,0), and it start off by heading right (x()+3). When it hits the far right(800,0), it turns its way to the left and keep moving (x()-3) until
    it hits the far left(0,0). Likewise, it heads right and keep moving(x()+3) until it hits the far right(800,0), it turns left and so on and so on in a straight and
    same route.</p>
  </blockquote>
  
  <p>So far,  the outcome is that <strong>my item only stays in the far right and never move.</strong></p>
  
  <p>And my only clue is that it has this warning in my "application output", but I guess this is probably not the point to this error.</p>
  
  <blockquote>
    <p>libpng warning: iCCP: known incorrect sRGB profile</p>
  </blockquote>
  
  <hr>
  
  <p>I code this way and these are my thoughts:</p>
  
  <ul>
  <li><p>use "while" in order to do this moving repeatedly.</p></li>
  <li><p>use bool judge to turn the moving direction of my item when it hits
  the far right or far left of the view.</p></li>
  <li><p>my pixmapitem's width is 100, so I write x()+100 in order to reach
  its right hand side when hitting the far right of the view.(my view's width is 800)</p></li>
  </ul>
  
  <p>Is there anything I'm missing? Any thoughts sharing or logic debugging will be very very appreciated, thanks!</p>
  
  <p>main window.cpp</p>
  
  <pre><code>#include "mainwindow.h"
  #include "ui_mainwindow.h"
  
  MainWindow::MainWindow(QWidget *parent):
      QMainWindow(parent),
      ui(new Ui::MainWindow),
      scene(new QGraphicsScene(0,0,800,600)),
      enemyMoveTimer(new QTimer)
  {
      ui-&gt;setupUi(this);
      ui-&gt;graphicsView-&gt;setScene(scene);
  
      //enemy
      Enemy * enemy = new Enemy();
      enemy-&gt;setPixmap(QPixmap(QPixmap(":/img/ghost.gif").scaled(100,100)));
      enemy-&gt;setPos(0,0);
      scene-&gt;addItem(static_cast&lt;QGraphicsPixmapItem*&gt;(enemy));
  
      enemyMoveTimer-&gt;start(100);
      enemy-&gt;connect(enemyMoveTimer, SIGNAL(timeout()), enemy, SLOT(enemyMove()));
  }
  </code></pre>
  
  <p>main window.h</p>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QMainWindow&gt;
  #include &lt;QGraphicsScene&gt;
  #include &lt;QGraphicsPixmapItem&gt;
  #include &lt;QTimer&gt;
  
  #include "enemy.h"
  
  
  namespace Ui {
  class MainWindow;
  }
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
  
  private:
      Ui::MainWindow *ui;
      QGraphicsScene *scene;
      QTimer *enemyMoveTimer;
  };
  
  #endif // MAINWINDOW_H
  </code></pre>
  
  <p>enemy.cpp</p>
  
  <pre><code>#include "enemy.h"
  
  Enemy::Enemy()
  {
  
  }
  
  void Enemy::enemyMove()
  {
       while(x()&gt;=0 &amp;&amp; x()+100&lt;=800)
       {
           if(judge==0)
           {
               if (x()+100==800)
                   judge=1;
  
               else
                   setPos(x() +3,y());
            }
  
           else if(judge==1)
            {
               if (x()==0)
                   judge=0;
  
               else
                   setPos(x() -3,y());
  
            }
       }
  }
  </code></pre>
  
  <p>enemy.h</p>
  
  <pre><code>#ifndef ENEMY_H
  #define ENEMY_H
  
  #include &lt;QObject&gt;
  #include &lt;QGraphicsPixmapItem&gt;
  #include &lt;QTimer&gt;
  
  class Enemy: public QObject,public QGraphicsPixmapItem
  {
      Q_OBJECT
  
  public:
      Enemy();
  
  
  public slots:
      void enemyMove();
  
  private:
        bool judge = 0;
  };
  
  #endif // ENEMY_H
  </code></pre>