title: Can't use QPainter in paintEvent of custom QWidget (Qt5)
body: <p>The error in question is as follows:</p>
  
  <pre><code>QWidget::paintEngine: Should no longer be called
  QPainter::begin: Paint device returned engine == 0, type: 1
  QPainter::setPen: Painter not active
  QPainter::setFont: Painter not active
  </code></pre>
  
  <p>After looking at a ton of online forum posts that all came down to people making the same mistake of trying to paint on their widgets outside of paintEvent(), I've been unable to have any luck. I tried drawing directly on this custom QWidget subclass, I've tried making a child QWidget and drawing on that. Can someone please show me what thing I'm (probably obviously to someone else) doing wrong?</p>
  
  <p>Thanks in advance.</p>
  
  <p>Header:</p>
  
  <pre><code>#ifndef TEXTDISPLAY_H
  #define TEXTDISPLAY_H
  
  #include &lt;QWidget&gt;
  
  class TextDisplay : public QWidget
  {
      Q_OBJECT
  
  public:
      TextDisplay(QString text, QString fontFamily = "Helvetica", int fontSize = 20,
                  int fontColor = Qt::black, QWidget* parent = 0);
  
  protected:
      void paintEvent(QPaintEvent *e) Q_DECL_OVERRIDE;
      void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE;
  
  private:
      QString text;
      QString fontFamily;
      int fontSize;
      int fontColor;
  };
  
  #endif // TEXTDISPLAY_H
  </code></pre>
  
  <p>Cpp:</p>
  
  <pre><code>#include "textdisplay.h"
  #include &lt;QPainter&gt;
  
  
  TextDisplay::TextDisplay(QString text, QString fontFamily, int fontSize,
                           int fontColor, QWidget* parent)
      : QWidget(parent), text(text), fontFamily(fontFamily),
        fontSize(fontSize), fontColor(fontColor)
  {
      this-&gt;setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
  }
  
  void TextDisplay::resizeEvent(QResizeEvent*) {
      paintEvent(NULL);
  }
  
  void TextDisplay::paintEvent(QPaintEvent *) {
      QPainter painter(this);
      painter.setPen(fontColor);
      painter.setFont(QFont(fontFamily, fontSize));
  
      QRect rect(QPoint(0, 0), this-&gt;size());
      QRect bound;
      QTextOption options;
      options.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
  
      painter.drawText(rect, Qt::TextWordWrap | Qt::TextWrapAnywhere, text, &amp;bound);
  
      this-&gt;setMinimumHeight(bound.size().height());
  }
  </code></pre>
  
  <p><strong>Edit with solution:</strong></p>
  
  <p>Thanks to both responders - I needed to do two things to get it working:</p>
  
  <p>a) Get rid of <code>paintEvent(NULL)</code>. I got rid of the resizeEvent override as well, it was unnecessary as suggested.</p>
  
  <p>b) Set a minimum size for the widget. Without this, the paintEvent was never called by Qt.</p>