×
Namespaces

Variants
Actions

How to take snapshot in Qt

From Nokia Developer Wiki
Jump to: navigation, search

This Qt snapshot app grabs the whole foreground window as a QPixmap. The image can be saved as a new .jpg image.

Needs-update.pngThis 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

Article Metadata
Code Example
Source file: Media:Snapshot.zip
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): Qt 4.5
Symbian
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

Screen01001.jpg

Zip file

Download Zip file : File:Snapshot.zip

This page was last modified on 11 October 2012, at 04:13.
183 page views in the last 30 days.
×