×
Namespaces

Variants
Actions
Revision as of 08:07, 16 September 2009 by valderind4 (Talk | contribs)

How to take snapshot in Qt

From Nokia Developer Wiki
Jump to: navigation, search
{{{width}}}


Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): S60 5th Edition
Symbian
Article
Keywords: QFile,QPixmap
Created: (17 Jul 2009)
Last edited: valderind4 (16 Sep 2009)


OverView

This is a snapshot application in Qt which grabs the window and make a photo snap of it.Here the pixmap allow you to grab the whole window. you can save this as a new jpg image.

Preconditions

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.
}
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());
}
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));
 
}

Screenshot

Screen01001.jpg

Zip file

Snapshot.zip

138 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×