title: How to take ScreenShot Qt/QML
body: <p><a href="http://www.developer.nokia.com/Community/Wiki/How_to_take_ScreenShot_Qt/QML" rel="noreferrer">QtQuick1 example</a>
  I can’t make it work on QtQuick2</p>
  
  <p>screenCapture.hpp</p>
  
  <pre><code>#include &lt;QObject&gt;
  
  class QString;
  class QQuickView;
  
  class screenCapture : public QObject
  {
      Q_OBJECT
  public:    
      explicit screenCapture(QQuickView *parent = 0);
  
  public slots:
      void capture(QString const &amp;path) const;
  
  private:
      QQuickView *currentView_;    
  };
  </code></pre>
  
  <p>screenCapture.cpp</p>
  
  <pre><code>#include &lt;QPixmap&gt;
  #include &lt;QQuickView&gt;
  #include &lt;QString&gt;
  
  #include "screenCapture.hpp"
  
  screenCapture::screenCapture(QQuickView *currentView) :
      QObject(0), currentView_(currentView)
  {
  }
  
  void screenCapture::capture(QString const &amp;path) const
  {
      QPixmap::grabWidget(currentView_).save(path);
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;QGuiApplication&gt;
  #include &lt;QQuickPaintedItem&gt;
  #include &lt;QQuickView&gt;
  #include &lt;QQmlContext&gt;
  
  #include "screenCapture.hpp"
  
  int main(int argc, char *argv[])
  {
      QGuiApplication app(argc, argv);
  
      qmlRegisterType&lt;screenCapture&gt;("Image", 1, 0, "ScreenCapture");
      qmlRegisterType&lt;saveAbleImage&gt;("Image", 1, 0, "SaveAbleImage");
  
      QQuickView view;
      view.setResizeMode(QQuickView::SizeRootObjectToView);
      view.setSource(QStringLiteral("/Users/Qt/program/experiment_apps_and_libs/funnyCamera/qml/funnyCamera/main.qml"));
      view.show();
  
      screenCapture screenClass(&amp;view);
      view.rootContext()-&gt;setContextProperty("screenObject", &amp;screenClass);
  
      return app.exec();
  }
  </code></pre>
  
  <p>main.qml</p>
  
  <pre><code>import QtQuick 2.0
  
  Rectangle{
      id : root
      width : 1024
      height : 768    
  
      MouseArea{
          anchors.fill: root
  
          onClicked: {          
              console.log("save image")
              screenObject.capture("Pictures/saveTest.jpg")
          }
      }
  }
  </code></pre>
  
  <p>error message</p>
  
  <p>QPixmap::grabWidget is deprecated, use QWidget::grab() instead
  QMetaObject::invokeMethod: No such method QQuickView::grab(QRect)</p>