Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Qt example 19: GUI around image rotation

 

This Qt example shows how to create a GUI around a program that displays a rotating image, like this screenshot (png).

 

 

Operating system: Ubuntu 10.04 LTS Lucid Lynx

IDE: Qt Creator 2.0.0

Project type: Qt4 GUI Application

Compiler: G++ 4.4.1

Libraries used:

 

 

 

 

 

Qt project file

 

#-------------------------------------------------
#
# Project created by QtCreator 2010-08-06T12:45:13
#
#-------------------------------------------------
QT += core gui
TARGET = CppQtExample19
TEMPLATE = app
SOURCES += main.cpp\
dialogmain.cpp
HEADERS += dialogmain.h
FORMS += dialogmain.ui
RESOURCES += \
resources.qrc

 

 

 

 

 

Source code

 

 

 

 

 

main.cpp

 

#include <QtGui/QApplication>
#include "dialogmain.h"

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  DialogMain w;
  w.show();
  return a.exec();
}

 

 

 

 

 

dialogmain.h

 

#ifndef DIALOGMAIN_H
#define DIALOGMAIN_H

#include <QDialog>

struct QGraphicsScene;
struct QGraphicsPixmapItem;

namespace Ui {
  class DialogMain;
}

class DialogMain : public QDialog
{
  Q_OBJECT

public:
  explicit DialogMain(QWidget *parent = 0);
  ~DialogMain();

protected:
  void changeEvent(QEvent *e);

private:
  Ui::DialogMain *ui;
  QTimer * const m_timer;
  QGraphicsScene * const m_scene;
  QGraphicsPixmapItem * const m_sprite;
  double m_angle;

  void resizeEvent(QResizeEvent*);
  void keyPressEvent(QKeyEvent* e);

private slots:
  void onTick();
  void onCheck();
  void onButtonLeft();
  void onButtonRight();
};

#endif // DIALOGMAIN_H

 

 

 

 

 

dialogmain.cpp

 

#include <cassert>
#include <cmath>
#include <iostream>

#include <QDesktopWidget>
#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include <QKeyEvent>
#include <QTimer>

#include "dialogmain.h"
#include "ui_dialogmain.h"

DialogMain::DialogMain(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::DialogMain),
  m_timer(new QTimer(this)),
  m_scene(new QGraphicsScene(this)),
  m_sprite(new QGraphicsPixmapItem),
  m_angle(0.0)
{
  ui->setupUi(this);

  //Load the sprite
  m_sprite->setPixmap(QPixmap(":/images/R.png"));
  assert(!m_sprite->pixmap().isNull());

  //Set the center of the sprite for rotation
  m_sprite->setTransformOriginPoint(
    static_cast<double>(m_sprite->pixmap().width()) / 2.0,
    static_cast<double>(m_sprite->pixmap().height()) / 2.0);

  //Add the sprite to the scene
  m_scene->addItem(m_sprite);

  //Add the scene to the view
  ui->graphicsView->setScene(m_scene);

  //Connect and start a timer
  QObject::connect(m_timer,SIGNAL(timeout()),this,SLOT(onTick()));
  QObject::connect(ui->check_auto_rotate,SIGNAL(clicked()),this,SLOT(onCheck()));
  QObject::connect(ui->button_left,SIGNAL(clicked()),this,SLOT(onButtonLeft()));
  QObject::connect(ui->button_right,SIGNAL(clicked()),this,SLOT(onButtonRight()));


  //Start the timer
  m_timer->start(100);

  this->setGeometry(0,0,
    static_cast<int>(640),
    static_cast<int>(640));
  const QRect screen = QApplication::desktop()->screenGeometry();
  this->move( screen.center() - this->rect().center() );
}

DialogMain::~DialogMain()
{
  delete ui;
  delete m_sprite;
}

void DialogMain::changeEvent(QEvent *e)
{
  QDialog::changeEvent(e);
  switch (e->type()) {
  case QEvent::LanguageChange:
    ui->retranslateUi(this);
    break;
  default:
    break;
  }
}

//Sets the scale of the maze
void DialogMain::resizeEvent(QResizeEvent*)
{
  const int size = std::max(
    m_sprite->pixmap().width(),
    m_sprite->pixmap().height());

  const double scale = 0.9
    * std::min(ui->graphicsView->width(),
        ui->graphicsView->height())
    / (static_cast<double>(size) * std::sqrt(2.0));
  m_sprite->setScale(scale);
}

void DialogMain::keyPressEvent(QKeyEvent* e)
{
  switch (e->key())
  {
    case Qt::Key_Left: case Qt::Key_Minus: case Qt::Key_A:
      m_angle-=1.0;
      m_sprite->setRotation(m_angle);
      break;
    case Qt::Key_Right: case Qt::Key_Plus: case Qt::Key_D:
      m_angle+=1.0;
      m_sprite->setRotation(m_angle);
      break;
  }
}

void DialogMain::onTick()
{
  m_angle+=1.0;
  m_sprite->setRotation(m_angle);
}

void DialogMain::onCheck()
{
  if (ui->check_auto_rotate->isChecked())
    m_timer->start();
  else
    m_timer->stop();
}

void DialogMain::onButtonLeft()
{
  m_angle-=1.0;
  m_sprite->setRotation(m_angle);
}

void DialogMain::onButtonRight()
{
  m_angle+=1.0;
  m_sprite->setRotation(m_angle);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict