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
This article explains how to take a screenshot of your view, using Qt/QML.
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
class ScreenClass : public QObject
Q_OBJECT public: Q_INVOKABLE void capture();
#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.
this->currentView = currentView;
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
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.
Q_DECL_EXPORT int main(int argc, char *argv)
QScopedPointer<QApplication> app(createApplication(argc, argv));
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.
id: rect; anchors.fill: parent; color: "red";
id: captureButton; anchors.centerin: parent
The image will be saved automatically to the path we have set in the class ScreenClass.cpp.