Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved]How to take ScreenShot Qt/QML
Forum Updated to NodeBB v4.3 + New Features

[Solved]How to take ScreenShot Qt/QML

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 7.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stereomatching
    wrote on last edited by
    #1

    "QtQuick1 example":http://www.developer.nokia.com/Community/Wiki/How_to_take_ScreenShot_Qt/QML

    I can't make it work on QtQuick2

    screenCapture.hpp
    @
    #include <QObject>

    class QString;
    class QQuickView;

    class screenCapture : public QObject
    {
    Q_OBJECT
    public:
    explicit screenCapture(QQuickView *parent = 0);

    public slots:
    void capture(QString const &path) const;

    private:
    QQuickView *currentView_;
    };
    @

    screenCapture.cpp
    @
    #include <QPixmap>
    #include <QQuickView>
    #include <QString>

    #include "screenCapture.hpp"

    screenCapture::screenCapture(QQuickView *currentView) :
    QObject(0), currentView_(currentView)
    {
    }

    void screenCapture::capture(QString const &path) const
    {
    QPixmap::grabWidget(currentView_).save(path);
    }
    @

    main.cpp
    @
    #include <QGuiApplication>
    #include <QQuickPaintedItem>
    #include <QQuickView>
    #include <QQmlContext>

    #include "screenCapture.hpp"

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    qmlRegisterType<screenCapture>("Image", 1, 0, "ScreenCapture");
    qmlRegisterType<saveAbleImage>("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(&view);
    view.rootContext()->setContextProperty("screenObject", &screenClass);
    
    return app.exec&#40;&#41;;
    

    }
    @

    main.qml
    @
    import QtQuick 2.0

    Rectangle{
    id : root
    width : 1024
    height : 768

    MouseArea{
        anchors.fill: root
    
        onClicked: {           
            console.log("save image"&#41;
            screenObject.capture("Pictures/saveTest.jpg"&#41;
        }
    }
    

    }
    @

    error message

    QPixmap::grabWidget is deprecated, use QWidget::grab() instead
    QMetaObject::invokeMethod: No such method QQuickView::grab(QRect)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stereomatching
      wrote on last edited by
      #2

      I found a solution, pretty simple, change the function "capture" to

      @
      void screenCapture::capture(QString const &path) const
      {
      QImage img = currentView_->grabWindow();
      img.save(path);
      }
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved