title: Paint method of QQuickPaintedItem not called
body: <p>I have a C++ QQuickPaintedItem which I want to instanciate in QML.
  However the paint() method is never called.</p>
  
  <p>Here's my code:</p>
  
  <p>CPlotXY.h</p>
  
  <pre><code>#ifndef CPLOTXY_H
  #define CPLOTXY_H
  
  #include &lt;QQuickPaintedItem&gt;
  #include &lt;lib/qcustomplot-source/qcustomplot.h&gt;
  #include &lt;QQuickView&gt;
  
  class CPlotXY : public QQuickPaintedItem
  {
      Q_OBJECT
      public:
          CPlotXY(QQuickItem* parent = 0);
          virtual ~CPlotXY();
  
          void paint(QPainter* painter);
  
          Q_INVOKABLE void init();
  
      protected:
          void routeMouseEvents( QMouseEvent* event );
  
          virtual void mousePressEvent( QMouseEvent* event );
          virtual void mouseReleaseEvent( QMouseEvent* event );
          virtual void mouseMoveEvent( QMouseEvent* event );
          virtual void mouseDoubleClickEvent( QMouseEvent* event );
  
          void setupQuadraticDemo( QCustomPlot* Q_CustomPlot );
  
      private:
          QCustomPlot*         Q_CustomPlot;
  
      private slots:
          void graphClicked( QCPAbstractPlottable* plottable );
          void onCustomReplot();
          void updateCustomPlotSize();
  
  };
  
  #endif // CPLOTXY_H
  </code></pre>
  
  <p>CPlotXY.cpp</p>
  
  <pre><code>#include "CPlotXY.h"
  
  CPlotXY::CPlotXY( QQuickItem* parent ) : QQuickPaintedItem( parent ), Q_CustomPlot( nullptr )
  {
      setFlag( QQuickItem::ItemHasContents, true );
      setAcceptedMouseButtons( Qt::AllButtons );
      connect( this, &amp;QQuickPaintedItem::widthChanged, this, &amp;CPlotXY::updateCustomPlotSize );
      connect( this, &amp;QQuickPaintedItem::heightChanged, this, &amp;CPlotXY::updateCustomPlotSize );
  }
  
  CPlotXY::~CPlotXY()
  {
      delete Q_CustomPlot;
      Q_CustomPlot = nullptr;
  }
  
  void CPlotXY::init()
  {
      Q_CustomPlot = new QCustomPlot();
  
      updateCustomPlotSize();
  
      setupQuadraticDemo( Q_CustomPlot );
  
      connect( Q_CustomPlot, &amp;QCustomPlot::afterReplot, this, &amp;CPlotXY::onCustomReplot );
  
      Q_CustomPlot-&gt;replot();
  
  }
  
  
  void CPlotXY::paint(QPainter* painter)
  {
      qDebug() &lt;&lt; "@####";
      if (Q_CustomPlot)
      {
          QPixmap    Q_PixMap( boundingRect().size().toSize() );
          QCPPainter Q_CPPainter( &amp;Q_PixMap );
  
          Q_CustomPlot-&gt;toPainter(&amp;Q_CPPainter);
  
          painter-&gt;drawPixmap(QPoint(), Q_PixMap);
      }
  }
  
  void CPlotXY::mousePressEvent( QMouseEvent* Q_Event )
  {
      qDebug() &lt;&lt; Q_FUNC_INFO;
      routeMouseEvents( Q_Event );
  }
  
  void CPlotXY::mouseReleaseEvent( QMouseEvent* Q_Event )
  {
      qDebug() &lt;&lt; Q_FUNC_INFO;
      routeMouseEvents( Q_Event );
  }
  
  void CPlotXY::mouseMoveEvent( QMouseEvent* Q_Event )
  {
      routeMouseEvents( Q_Event );
  }
  
  void CPlotXY::mouseDoubleClickEvent( QMouseEvent* Q_Event )
  {
      qDebug() &lt;&lt; Q_FUNC_INFO;
      routeMouseEvents( Q_Event );
  }
  
  void CPlotXY::graphClicked( QCPAbstractPlottable* Q_Plottable )
  {
      qDebug() &lt;&lt; Q_FUNC_INFO &lt;&lt; QString( "Clicked on graph '%1 " ).arg( Q_Plottable-&gt;name() );
  }
  
  void CPlotXY::routeMouseEvents( QMouseEvent* Q_Event )
  {
      if (Q_CustomPlot)
      {
          QMouseEvent* Q_NewEvent = new QMouseEvent( Q_Event-&gt;type(), Q_Event-&gt;localPos(), Q_Event-&gt;button(), Q_Event-&gt;buttons(), Q_Event-&gt;modifiers() );
          //QCoreApplication::sendEvent( m_CustomPlot, newEvent );
          QCoreApplication::postEvent( Q_CustomPlot, Q_NewEvent );
      }
  }
  
  void CPlotXY::updateCustomPlotSize()
  {
      if (Q_CustomPlot)
      {
          Q_CustomPlot-&gt;setGeometry( 0, 0, width(), height() );
      }
  }
  
  void CPlotXY::onCustomReplot()
  {
      qDebug() &lt;&lt; Q_FUNC_INFO;
      update();
  }
  
  void CPlotXY::setupQuadraticDemo( QCustomPlot* customPlot )
  {
      // make top right axes clones of bottom left axes:
      QCPAxisRect* axisRect = customPlot-&gt;axisRect();
  
      // generate some data:
      QVector&lt;double&gt; x( 101 ), y( 101 );   // initialize with entries 0..100
      QVector&lt;double&gt; lx( 101 ), ly( 101 ); // initialize with entries 0..100
      for (int i = 0; i &lt; 101; ++i)
      {
          x[i] = i / 50.0 - 1;              // x goes from -1 to 1
          y[i] = x[i] * x[i];               // let's plot a quadratic function
  
          lx[i] = i / 50.0 - 1;             //
          ly[i] = lx[i];                    // linear
      }
      // create graph and assign data to it:
      customPlot-&gt;addGraph();
      customPlot-&gt;graph( 0 )-&gt;setPen( QPen( Qt::red ) );
      //customPlot-&gt;graph( 0 )-&gt;setSelectedPen( QPen( Qt::blue, 2 ) );
      customPlot-&gt;graph( 0 )-&gt;setData( x, y );
  
      customPlot-&gt;addGraph();
      customPlot-&gt;graph( 1 )-&gt;setPen( QPen( Qt::magenta ) );
      //customPlot-&gt;graph( 1 )-&gt;setSelectedPen( QPen( Qt::blue, 2 ) );
      customPlot-&gt;graph( 1 )-&gt;setData( lx, ly );
  
      // give the axes some labels:
      customPlot-&gt;xAxis-&gt;setLabel( "x" );
      customPlot-&gt;yAxis-&gt;setLabel( "y" );
      // set axes ranges, so we see all data:
      customPlot-&gt;xAxis-&gt;setRange( -1, 1 );
      customPlot-&gt;yAxis-&gt;setRange( -1, 1 );
  
      customPlot -&gt;setInteractions( QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables );
      connect( customPlot, SIGNAL( plottableClick( QCPAbstractPlottable*, QMouseEvent* ) ), this, SLOT( graphClicked( QCPAbstractPlottable* ) ) );
  }
  </code></pre>
  
  <p>XPlotXY.qml</p>
  
  <pre><code>import QtQuick 2.0
  import CPlotXY 1.0
  
  Item {
      CPlotXY {
          id: customPlot
          anchors.fill: parent
          Component.onCompleted: init()
      }
  }
  </code></pre>
  
  <p>And Finally here is when I instanciate it:</p>
  
  <pre><code>Repeater {
      id: widgetRepeater
      model: pageModel
      anchors.fill: parent
      delegate: Loader {
  
  
  
          property int modelX: model.XPos
          property int modelY: model.YPos
          //property int modelWidth: model.width
          //property int modelHeight: model.height
  
          active: true;
          asynchronous: true
          //anchors.centerIn:  parent
          source: "%1.qml".arg(model.Type)
      }
  }
  </code></pre>
  
  <p>The init() function of the CPlotXY is called and the object is created but paint() method is never called.
  I know the solution is obvious but i'm not comfortable with QQuickPaintedItem.</p>
  
  <p>Thank you for helping.</p>