Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

How to take ScreenShot Qt/QML

From Wiki
Jump to: navigation, search

This article explains how to take a screenshot of your view, using Qt/QML.

Article Metadata
Tested with
Devices(s): Nokia N8, Nokia 701
Platform(s): Symbian^3 and later, Qt 4.7 and later
Keywords: screenshot,Qt, Qml, screen
Created: manu91 (20 Jun 2012)
Last edited: hamishwillee (14 Jun 2013)



In this article we will explain how to take a screenshot of your current view using Qt/QML. To do this we will use the library QPixmap, that provides an easy way to take a snapshot of our current declarative view and then to save it.

In this way it's possible only to take a screenshot of our application!


First of all we have to create a header file for our class

#include <QDebug>
#include <QPixmap>
#include <qmlapplicationviewer.h>
#include <QDir>
class ScreenClass : public QObject
ScreenClass(QmlApplicationViewer* currentView);
Q_OBJECT public: Q_INVOKABLE void capture();
QmlApplicationViewer* currentView;
#endif // SCRENCLASS_H

Here we have declared the class constructor to take the QmlApplicationViewer's object named "currentView" as reference. We have also defined the method "capture()" as Q_INVOKABLE, in this way it's possible to call it from QML.


Here we implement our class.

#include "ScreenClass.h"
ScreenClass::ScreenClass(QmlApplicationViewer *currentView){
this->currentView = currentView;
void ScreenClass::capture(){
#if defined(Q_OS_SYMBIAN)
QPixmap::grabWidget(currentView).save("E:/Images/" + FILE_NAME + ".png");
//QPixmap::grabWidget(currentView).save( QDir::currentPath() + "/screenShot/" + FILE_NAME+ ".png");
qDebug() << "Captured on Symbian, here" + "E:/Images/";
#endif // Q_OS_SYMBIAN
#if defined(Q_WS_SIMULATOR)
QPixmap::grabWidget(currentView).save( QDir::currentPath() + "/screenShot/" + FILE_NAME+ ".png");
qDebug() << "Captured on Simulator, here: " + QDir::currentPath() + "/screenShot/";
#endif // Q_OS_SYMBIAN

With QDir::currentPath() we can save the image in the default directory of our application, but we can also set an absolute path.


#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QDeclarativeContext>
#include "ScreenClass.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
QScopedPointer<QApplication> app(createApplication(argc, argv));
QmlApplicationViewer viewer;
ScreenClass screenClass(&viewer);
viewer.rootContext()->setContextProperty("screenObject", &screenClass);
return app->exec();

In "main.cpp" we set viewer as reference for ScreenClass, and with "setContextProperty" we make the class visible form QML.

It' almost done, now we can use our method from QML.


Page {
id: mainPage
Rectangle {
id: rect; anchors.fill: parent; color: "red";
Button {
id: captureButton; anchors.centerin: parent
text: "Capture!!!"
onClicked: screenObject.capture();

The image will be saved automatically to the path we have set in the class ScreenClass.cpp.

This page was last modified on 14 June 2013, at 05:06.
191 page views in the last 30 days.