This is the example of QtQuick1
http://www.developer.nokia.com/Commu...eenShot_Qt/QML
I can’t make it work on QtQuick2
screenCapture.hpp
#include <QObject>
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_;
};
#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_;
};
To copy to clipboard, switch view to plain text mode
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
);
}
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);
}
To copy to clipboard, switch view to plain text mode
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();
}
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();
}
To copy to clipboard, switch view to plain text mode
main.qml
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")
}
}
}
main.qml
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")
}
}
}
To copy to clipboard, switch view to plain text mode
error message
QPixmap::grabWidget is deprecated, use QWidget::grab() instead
QMetaObject::invokeMethod: No such method QQuickView::grab(QRect)
Added after 1 2 minutes:
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);
}
void screenCapture::capture(QString const &path) const
{
QImage img = currentView_->grabWindow();
img.save(path);
}
To copy to clipboard, switch view to plain text mode
Bookmarks