title: How to display a QPixmap in a Qt Quick Image?
body: <p>I'm working on a small Qt Quick application with QML and C++ for taking screenshots. The grabWindow() function returns a QPixmap. I would like to display this screenshot in a Qt Quick Image, but Image only accepts URLs. How do I display my QPixmap without having to save it to a path first?</p>
  
  <p>My screenshot.cpp:</p>
  
  <pre><code>#include &lt;QtQuick&gt;
  #include "screenshot.h"
  
  Screenshot::Screenshot() {}
  
  QPixmap *Screenshot::grabScreen() {
    QScreen *screen = QGuiApplication::primaryScreen();
    if(screen)
      *originalPixmap = screen-&gt;grabWindow(0);
    return originalPixmap;
  }
  </code></pre>
  
  <p>screenshot.h:</p>
  
  <pre><code>#ifndef SCREENSHOT_H
  #define SCREENSHOT_H
  
  #include &lt;QObject&gt;
  class QString;
  class QQuickView;
  
  class Screenshot : public QObject
  {
    Q_OBJECT
    public:
      Screenshot();
      Q_INVOKABLE QPixmap *grabScreen();
    public slots:
    private:
      QPixmap *originalPixmap;
  };
  
  #endif // SCREENSHOT_H
  </code></pre>
  
  <p>main.qml:</p>
  
  <pre><code>import QtQuick 2.5
  import QtQuick.Window 2.2
  import QtQuick.Controls 1.4
  import QtQuick.Layouts 1.1
  import QtQuick.Controls.Styles 1.4
  import com.pixmate.screenshot 1.0
  
  ApplicationWindow {
      visible: true
      width: 420
      height: 380
      menuBar: MenuBar {
          Menu {
              title: "Screen"
              MenuItem {
                  text: "New Screenshot"
                  shortcut: "Ctrl+Shift+3"
              }
              MenuItem {
                  text: "Save Screenshot"
                  shortcut: "Ctrl+Shift+4"
              }
          }
  
          Menu {
              title: "Edit"
              MenuItem { text: "Cut" }
              MenuItem { text: "Copy" }
              MenuItem { text: "Paste" }
          }
      }
  
      Screenshot {
          id: screenShot
      }
  
      Image {
          id: screenshotImage
          visible: true
          anchors.horizontalCenter: parent.horizontalCenter
          y: 10
          width: 240
          height: 160
          source: "&lt;insert URL here&gt;"
      }
  
      GroupBox {
          title: "Options"
          y: 180
          x: 10
          width: parent.width-20
          Column {
              spacing: 10
              Label {
                  text: "Screenshot Delay"
              }
              SpinBox {
                  maximumValue: 120
                  value: 3
                  suffix: " s"
                  width: 80
                  onValueChanged: {
                      if(value == 0) {
                          hideThisWindowCheckbox.enabled = false;
                          hideThisWindowCheckbox.checked = false;
                      } else {
                          hideThisWindowCheckbox.enabled = true;
                      }
                  }
              }
              CheckBox {
                  id: hideThisWindowCheckbox
                  text: "Hide this window"
                  checked: true
                  /*style : CheckBoxStyle {
                      padding.top: 10
                  }*/
              }
          }
          Column {
              x: 185
              spacing: 10
              RadioButton {
                  id: captureScreenRadioButton
                  text: "Capture entire screen"
                  checked: true
                  onClicked: {
                      captureActiveWindowRadioButton.checked = false
                      captureScreenSelectionRadioButton.checked = false
                  }
              }
              RadioButton {
                  id: captureActiveWindowRadioButton
                  text: "Capture active window"
                  enabled: false // disabled for now
                  onClicked: {
                      captureScreenRadioButton.checked = false
                      captureScreenSelectionRadioButton.checked = false
                  }
              }
              RadioButton {
                  id: captureScreenSelectionRadioButton
                  text: "Capture selected area"
                  enabled: false // disabled for now
                  onClicked: {
                      captureScreenRadioButton.checked = false
                      captureActiveWindowRadioButton.checked = false
                  }
              }
          }
      }
      Button {
          id: newScreenshotButton
          text: "New Screenshot"
          y: 320
          x: 10
          onClicked: {
              screenshotImage.data = screenShot.grabScreen();
          }
      }
  
      Button {
          id: saveScreenshotButton
          text: "Save Screenshot"
          y: 320
          x: 165
      }
      Button {
          id: quitButton
          text: "Quit"
          y: 320
          x: 320
      }
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;QGuiApplication&gt;
  #include &lt;QQmlContext&gt;
  #include &lt;QQuickView&gt;
  #include &lt;QQmlApplicationEngine&gt;
  #include "src/screenshot.h"
  
  int main(int argc, char *argv[])
  {
      QGuiApplication app(argc, argv);
  
      qmlRegisterType&lt;Screenshot&gt;("com.pixmate.screenshot", 1, 0, "Screenshot");
  
      QQmlApplicationEngine engine;
      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
  
      return app.exec();
  }
  </code></pre>
  
  <p>edit:
  When I run it as it is right now, I get</p>
  
  <blockquote>
    <p>qrc:/main.qml:126: Error: Unknown method return type: QPixmap*</p>
  </blockquote>
  
  <p>Thanks in advance!</p>