title: Using Qt Undo Framework With QPainter
body: <p>I am creating a drawing application with Qt and want to include undo and redo commands for whatever is drawn on a QImage. For this, I want to at least try and incorporate the Qt undo framework, since manually doing it will probably be above my skill level. 
  I have a MainWindow class where the main work is done and where I have the menu with undo and redo QActions. 
  The painting is done in a QWidget class called DrawingArea like this (PenShape is an enum of shapes):</p>
  
  <pre><code>void DrawingArea::drawLine(const QPoint &amp;endPoint) {
  QPainter painter(&amp;image);
  
  //some pen config code
  
  switch (currentShape){
  case PenShape::Polygon:
      painter.drawPolygon(&amp;endPoint, 5, Qt::OddEvenFill);
      break;
  case PenShape::Line:
      painter.drawLine(lastPoint, endPoint);
      break;
  //other shapes and their draw methods..
  }
  update()
  }
  </code></pre>
  
  <p>So as can be seen, the painter can draw in multiple shapes. I know I would have to somehow transfer this to a QUndoCommand class, but how? The drawline method is called only in DrawingArea's mouse event methods like this:</p>
  
  <pre><code>void DrawingArea::mousePressEvent(QMouseEvent *event){
  if (event-&gt;button() == Qt::LeftButton) {
          lastPoint = event-&gt;pos();
      }
  }
  </code></pre>
  
  <p>The QtUndoStack is located in MainWindow and the QActions are also there, but somehow I would have to connect the drawLine QtUndoCommand class and the mousePressEvent and mainWindow QAction with its connected slot method all together. This is what I don't know how to do.</p>
  
  <p>Any help is appreciated!</p>
  
  <p>Edit, here's the menu button actions:</p>
  
  <pre><code>m_undo = undoStack-&gt;createUndoAction(this, tr("&amp;Undo"));
  m_undo-&gt;setShortcuts(QKeySequence::Undo);
  
  m_redo = undoStack-&gt;createRedoAction(this, tr("&amp;Redo"));
  m_redo-&gt;setShortcuts(QKeySequence::Redo);
  </code></pre>
  
  <p>I have no QUndoCommand classes because I'm unsure of how to create them yet. 
  In my MainWindow, I have a undoStack declared like this:</p>
  
  <pre><code>undoStack = new QUndoStack(this); 
  </code></pre>