title: How to draw a Rectangle in Qgraphicsview dynamically (runtime), using Qt4
body: <p>using QT Creator version 2.4.1.</p>
  
  <p>I'm just a starter on QT platform and trying to draw a Rectangle Dynamically. The program uses 3 Mouse events namely </p>
  
  <ul>
  <li>mousePressEvent </li>
  <li>mouseMoveEvent</li>
  <li>mouseReleaseEvent</li>
  </ul>
  
  <p>Problems is <strong>mouseMoveEvent</strong> is never called.</p>
  
  <p>Here is the code snippet</p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;QtGui/QApplication&gt;
  #include "dialog.h"
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      Dialog w;
      w.show();
  
      return a.exec();
  }
  </code></pre>
  
  <p><strong>Dialog.cpp</strong></p>
  
  <pre><code>#include "dialog.h"
  #include "ui_dialog.h"
  #include "mysquare.h"
  
  Dialog::Dialog(QWidget *parent) :
      QDialog(parent),
      ui(new Ui::Dialog)
  {
      ui-&gt;setupUi(this);
      setMouseTracking(true);
      scene = new QGraphicsScene(this);
      ui-&gt;graphicsView-&gt;setScene(scene);
      scene-&gt;setBackgroundBrush(Qt::black);
      square = new mySquare;
      scene-&gt;addItem(square);
      square-&gt;mPix = QPixmap(200,200);
      square-&gt;mPix.fill(Qt::white);
      scene-&gt;addPixmap(square-&gt;mPix);
  }
  
  Dialog::~Dialog()
  {
      delete ui;
  }
  
  void Dialog::on_pushButton_clicked()
  {
      square-&gt;selectedTool = 1;
  }
  </code></pre>
  
  <p><strong>Dialog.h</strong></p>
  
  <pre><code>#ifndef DIALOG_H
  #define DIALOG_H
  
  #include &lt;QDialog&gt;
  #include &lt;QtCore&gt;
  #include &lt;QtGui&gt;
  #include "mysquare.h"
  
  namespace Ui {
  class Dialog;
  }
  
  class Dialog : public QDialog
  {
      Q_OBJECT
  
  public:
      explicit Dialog(QWidget *parent = 0);
      ~Dialog();
  
  private slots:
      void on_pushButton_clicked();
  
  private:
      Ui::Dialog *ui;
      QGraphicsScene *scene;
      mySquare *square;
  
  };
  
  #endif // DIALOG_H
  </code></pre>
  
  <p><strong>mysquare.cpp</strong></p>
  
  <pre><code>#include "mysquare.h"
  
  using namespace std;
  
  
  mySquare::mySquare()
  {
      pressed  = false;
      selectedTool = 1;
      mPix = QPixmap(200,200);
      mPix.fill(Qt::white);
  }
  
  void mySquare::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  {
      painter-&gt;begin(this);
      if(pressed)
      {
          painter-&gt;drawPixmap(0,0,mPix);
          painter-&gt;drawRect(mRect);
          drawStarted = true;
      }
      else if (drawStarted)
      {
          QPainter tempPainter(&amp;mPix);
          tempPainter.drawRect(mRect);
          painter-&gt;drawPixmap(0,0,mPix);
      }
  
      painter-&gt;end();
  }
  
  void mySquare::mousePressEvent(QGraphicsSceneMouseEvent *event)
  {
      pressed = true;
  
      y = event-&gt;pos();
      cout&lt;&lt;"Pos X : "&lt;&lt;y.x() &lt;&lt;endl;
      cout&lt;&lt;"Pos Y : "&lt;&lt;y.y() &lt;&lt;endl;
  
      if(selectedTool == 1)
      {
          mRect.setTopLeft(event-&gt;pos());
          mRect.setBottomRight(event-&gt;pos());
  
          cout &lt;&lt; "Value of x_start axis " &lt;&lt; X_1 &lt;&lt;endl;
          cout &lt;&lt; "Value of y_start axis " &lt;&lt; Y_1 &lt;&lt;endl;
      }
      else if (selectedTool == 2)
      {
  
      }
      update();
  }
  
  void mySquare::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
  {
      cout&lt;&lt;"Inside mouseMoveEvent \n";
      if(event-&gt;type() == QEvent::MouseMove)
      {
          if(selectedTool == 1){
              mRect.setBottomRight(event-&gt;pos());
          }
          else if (selectedTool == 2){
              //mLine.setP2(event-&gt;pos());
          }
      }
      update();
  }
  
  void mySquare::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
  {
      pressed = false;
      update();
      cout&lt;&lt;"Inside mouseReleaseEvent \n";
  }
  </code></pre>
  
  <p><strong>mysquare.h</strong></p>
  
  <pre><code>#ifndef MYSQUARE_H
  #define MYSQUARE_H
  
  #include &lt;QPainter&gt;
  #include &lt;QGraphicsItem&gt;
  #include &lt;QDebug&gt;
  #include &lt;QPainter&gt;
  #include &lt;iostream&gt;
  #include &lt;QPixmap&gt;
  #include &lt;QPaintEvent&gt;
  #include &lt;QGraphicsSceneMouseEvent&gt;
  
  class mySquare: public QGraphicsItem
  {
  public:
      mySquare();
      int selectedTool;
      QPixmap mPix;
      QRectF boundingRect() const;
      void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
      bool pressed;
      bool drawStarted;
  
  protected:
      void mousePressEvent(QGraphicsSceneMouseEvent *event);
      void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
      void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
  
  private:
      QRectF mRect;
      QPointF y;
  
  
  private slots:
  
  };
  
  #endif // MYSQUARE_H
  </code></pre>
  
  <p>Please guide me,
  Thanks.</p>