How to take snapshot in Qt
This Qt snapshot app grabs the whole foreground window as a QPixmap. The image can be saved as a new .jpg image.
This article needs to be updated: If you found this article useful, please fix the problems below then delete the {{ArticleNeedsUpdate}} template from the article to remove this warning.
Reasons: hamishwillee (17 Feb 2012)
Example is hosted within framework of deprecated QWidget based UI. Would be good to modify to a Qt Quick app
Reasons: hamishwillee (17 Feb 2012)
Example is hosted within framework of deprecated QWidget based UI. Would be good to modify to a Qt Quick app
Article Metadata
Code Example
Source file: Media:Snapshot.zip
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): Qt 4.5
Article
Keywords: QFile, QPixmap
Created: mind_freak
(17 Jul 2009)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Sourcecode
Header File
#ifndef SHOOT_H
#define SHOOT_H
#include <QtGui/QWidget>
#include<QPushButton>
#include<QFileDialog>
#include<QLabel>
#include<QFile>
#include<QDir>
#include<QDesktopWidget>
#include<QVBoxLayout>
#include<QPixmap>
class shoot : public QWidget
{
Q_OBJECT
public:
shoot(QWidget *parent = 0);
~shoot();
private slots:
void shotscreen();
void saveScreen();
private:
void updateLabel();
QPixmap originalPixmap;
QVBoxLayout *lay;
QLabel *pixmap;
QPushButton *shootin;
QPushButton *save;
QPushButton *quit;
};
#endif // SHOOT_H
Source File
#include "shoot.h"
#include "ui_shoot.h"
shoot::shoot(QWidget *parent)
: QWidget(parent)
{
pixmap = new QLabel(this);
pixmap->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
pixmap->setAlignment(Qt::AlignCenter);
pixmap->setMinimumSize(240, 160);
shootin=new QPushButton("shoot",this);
save=new QPushButton("Save",this);
quit=new QPushButton("Quit",this);
connect(shootin,SIGNAL(clicked()),this,SLOT(shotscreen()));
connect(save,SIGNAL(clicked()),this,SLOT(saveScreen()));
connect(quit,SIGNAL(clicked()),qApp, SLOT(quit()));
lay=new QVBoxLayout(this);
lay->addWidget(pixmap);
lay->addWidget(shootin);
lay->addWidget(save);
lay->addWidget(quit);
setLayout(lay);
showMaximized();
setStyleSheet("* { background-color:rgb(0,0,0);color:rgb(255,100,50); padding: 7px}}");
}
shoot::~shoot()
{
// No need to delete any object that has got a parent which is properly deleted.
}
In the constructor we set up a layout into which we place the pixmap (preview) and three buttons (shoot,save and quit). The preview is always visible and we set it to a fixed size of 240x160.
void shoot::saveScreen()
{
QString format = "png";
QString initialPath = QDir::currentPath() + tr("/untitled.") + format;
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),initialPath,tr("%1 Files (*.%2);;All Files (*)").arg(format.toUpper()).arg(format));
if (!fileName.isEmpty())
originalPixmap.save(fileName, format.toAscii());
}
The saveScreen() is used to save the screenshot.
void shoot::shotscreen()
{
originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
qApp->beep();
updateLabel();
}
void shoot::updateLabel()
{
pixmap->setPixmap(originalPixmap.scaled(pixmap->size(), Qt::KeepAspectRatio,Qt::SmoothTransformation));
}
QPixmap provides the static method grabWindow() which is a window ID. With grabWindow() we can obtain the screenshot and store it in originalPixmap. In updateLabel() function a pixmap scaled down to the size of the label is also included in the pixmap (preview)
Screenshot
Zip file
Download Zip file : File:Snapshot.zip



12 Sep
2009
The article seems very intereating. The application presented here grabs the photo-snap of the whole window of your device. The platform used is S60 5th edition and the application is in Qt. The representation od the content is in a systematic manner. The source-code given is in a simple manner. Moreover hte screen-shot of the application is also given.
It is useful to the beginners and intermediate developers for their further progress.
21 Sep
2009
This article is good explained with snapshot so every one can imagine what it will look like.In this article we can take snap shot of a diaplay of an application so that every one can take snapshot of their application displays and they can put this snapshots to their application.
i want to ask a question that can it be possible to run this application in background mode? then every one can take snapshot of the second running application.
26 Sep
2009
Taking screen-shot in smart-phone is equally important to taking screen-shot in desktop system. Developer find it more useful to show initial layout of application during development phase. Qt have powerful API, called QPixmap::grabWindow(), to capture screen-shot of device.
This article describes how to take screen-shots using QPixmap::grabWindow() API. Source code with source and header file will helps developer to create similar application. Although article have less explanation, but it does not reduce importance of article.