×
Namespaces

Variants
Actions

QtRotateImage

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Compatibility
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
Symbian
Article
Keywords: QPixmap, QSignalMapper, QFileDialog::getOpenFileName, QFileDialog::getSaveFileName
Created: ebra (15 Jun 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

Overview

This code example shows how to work with QPixmap class. User can load any image, rotate it and then save it with a new name.

This snippet can be self-signed. As it does not use any API which require developer/certified signing.

Preconditions

  • Download and Install the Qt SDK


Images

Qt07.png Qt08.png

Source

QtRotateImage.zip

#include <QtGui/QApplication>
#include "mainwindow.h"
 
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QtGui/QMainWindow>
#include <QSignalMapper>
#include <QPixmap>
 
namespace Ui
{
class MainWindow;
}
 
class MainWindow : public QMainWindow
{
Q_OBJECT
 
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
 
private slots:
void loadImage();
void saveImage();
void rotateImage(int angle);
 
private:
Ui::MainWindow *ui;
QSignalMapper *rotateMapper;
QPixmap pixmap;
};
 
#endif // MAINWINDOW_H
#include <QFileDialog>
#include <QPixmap>
 
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
 
rotateMapper = new QSignalMapper(this);
 
connect(ui->actionLoad, SIGNAL(triggered()), this, SLOT(loadImage()));
connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(saveImage()));
 
connect(ui->actionRotate_90, SIGNAL(triggered()), rotateMapper, SLOT(map()));
connect(ui->actionRotate_180, SIGNAL(triggered()), rotateMapper, SLOT(map()));
connect(ui->actionRotate_270, SIGNAL(triggered()), rotateMapper, SLOT(map()));
 
rotateMapper->setMapping(ui->actionRotate_90, 90);
rotateMapper->setMapping(ui->actionRotate_180, 180);
rotateMapper->setMapping(ui->actionRotate_270, 270);
 
connect(rotateMapper, SIGNAL(mapped(int)), this, SLOT(rotateImage(int)));
}
MainWindow::~MainWindow()
{
if(rotateMapper)
{
delete rotateMapper;
}
delete ui;
}
void MainWindow::loadImage()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), "/", tr("Image Files (*.png *.jpg *.bmp)"));
if ( fileName.isNull() == false )
{
if ( pixmap.load(fileName) == true )
{
ui->label->setPixmap(pixmap);
}
}
}
void MainWindow::saveImage()
{
if ( pixmap.isNull() == false )
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save Image"), "/", tr("Image Files (*.png *.jpg *.bmp)"));
if ( fileName.isNull() == false )
{
pixmap.save(fileName);
}
}
}
void MainWindow::rotateImage(int angle)
{
int h = pixmap.height();
int w = pixmap.width();
 
QPixmap p = pixmap.transformed(
QTransform().translate(w/2, h/2).rotate(angle).translate(-w/2, -h/2));
ui->label->setPixmap(p);
pixmap=p;
}

External Links

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