//main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.setFixedSize(240,320);
//w.setMinimumSize(240,320);
w.show();
return a.exec();
}
//main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setFixedSize(240,320);
//w.setMinimumSize(240,320);
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
//mainwindow.cpp
#include "mainwindow.h"
#include <QDebug>
#include <QAction>
#include <QFile>
bool zoomIn=false;
PictureItem
::PictureItem(QSizeF size,
const QPixmap
& pixmap,
QObject* parent
) :{
m_size = size;
setCacheMode(DeviceCoordinateCache);
}
PictureItem::~PictureItem()
{
}
QRectF PictureItem
::boundingRect() const {
return QRectF(0,
0, m_size.
width(), m_size.
height());
}
void PictureItem::animatePosition(const QPointF& start, const QPointF& end)
{
// Start animate this class
QPropertyAnimation* anim = new QPropertyAnimation(this, "pos");
// 2 second duration animation
anim->setDuration(500);
// position to start animation
anim->setStartValue(start);
// end position of animation
anim->setEndValue(end);
// easing curve
anim->setEasingCurve(QEasingCurve::OutQuad);
// Listen animation finished signal
QObject::connect(anim,
SIGNAL(finished
()),
this,
SLOT(animationFinished
()));
// Start animation and delete QPropertyAnimation class on the end
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
void PictureItem::animateScale(qreal start, qreal end)
{
// Start animate this class
QPropertyAnimation* anim = new QPropertyAnimation(this, "scale");
anim->setDuration(1000);
anim->setStartValue(start);
anim->setEndValue(end);
anim->setEasingCurve(QEasingCurve::InOutBack);
QObject::connect(anim,
SIGNAL(finished
()),
this,
SLOT(animationFinished
()));
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
void PictureItem::animationFinished()
{
qDebug()<<"Animation Finished!";
}
MainWindow
::MainWindow(QWidget *parent
){
// Create QGraphicsScene and QGraphicsView
m_view
->setViewportUpdateMode
(QGraphicsView::BoundingRectViewportUpdate);
m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// Create PictureItem for the animation
m_item = new PictureItem(pixmap.size(),pixmap,this);
// Add PictureItem to center of the scene
m_scene->addItem(m_item);
m_item->setScale(0.5);
createMenu();
this->setCentralWidget(m_view);
}
MainWindow::~MainWindow()
{
}
{
// Update scene rect
m_scene->setSceneRect(m_view->rect());
// Calculate center point for the picture
m_center
= QPointF((rect
().
width()-m_item
->boundingRect
().
width()*m_item
->scale
())/2,
(rect().height()-m_item->boundingRect().height()*m_item->scale())/2 );
// Set picture to center of the sceen
m_item->setPos(m_center);
}
{
// Set picture to center of the sceen
m_item->setPos(m_center);
m_item->setScale(0.5);
}
void MainWindow::createMenu()
{
QMenu* menu
= this
->menuBar
()->addMenu
("Animate");
menu->addAction("Position", this, SLOT(animatePos()));
menu->addAction("Scale", this, SLOT(animateScale()));
}
void MainWindow::animatePos()
{
// Animate picture position
m_item
->animatePosition
(m_item
->pos
(),
QPoint(0-this
->rect
().
size().
width(),
0));
}
void MainWindow::animateScale()
{
m_item->animateScale(1.0,2.0);
}
//mainwindow.cpp
#include "mainwindow.h"
#include <QDebug>
#include <QAction>
#include <QFile>
bool zoomIn=false;
PictureItem::PictureItem(QSizeF size, const QPixmap& pixmap, QObject* parent) :
QObject(parent), QGraphicsPixmapItem(pixmap)
{
m_size = size;
setCacheMode(DeviceCoordinateCache);
}
PictureItem::~PictureItem()
{
}
QRectF PictureItem::boundingRect() const
{
return QRectF(0, 0, m_size.width(), m_size.height());
}
void PictureItem::animatePosition(const QPointF& start, const QPointF& end)
{
// Start animate this class
QPropertyAnimation* anim = new QPropertyAnimation(this, "pos");
// 2 second duration animation
anim->setDuration(500);
// position to start animation
anim->setStartValue(start);
// end position of animation
anim->setEndValue(end);
// easing curve
anim->setEasingCurve(QEasingCurve::OutQuad);
// Listen animation finished signal
QObject::connect(anim, SIGNAL(finished()), this, SLOT(animationFinished()));
// Start animation and delete QPropertyAnimation class on the end
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
void PictureItem::animateScale(qreal start, qreal end)
{
// Start animate this class
QPropertyAnimation* anim = new QPropertyAnimation(this, "scale");
anim->setDuration(1000);
anim->setStartValue(start);
anim->setEndValue(end);
anim->setEasingCurve(QEasingCurve::InOutBack);
QObject::connect(anim, SIGNAL(finished()), this, SLOT(animationFinished()));
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
void PictureItem::animationFinished()
{
qDebug()<<"Animation Finished!";
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// Create QGraphicsScene and QGraphicsView
m_scene = new QGraphicsScene(this);
m_view = new QGraphicsView(m_scene,this);
m_view->setCacheMode(QGraphicsView::CacheBackground);
m_view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// Create PictureItem for the animation
QPixmap pixmap(":/qt1.jpg");
m_item = new PictureItem(pixmap.size(),pixmap,this);
// Add PictureItem to center of the scene
m_scene->addItem(m_item);
m_item->setScale(0.5);
createMenu();
this->setCentralWidget(m_view);
}
MainWindow::~MainWindow()
{
}
void MainWindow::resizeEvent(QResizeEvent *)
{
// Update scene rect
m_scene->setSceneRect(m_view->rect());
// Calculate center point for the picture
m_center = QPointF((rect().width()-m_item->boundingRect().width()*m_item->scale())/2,
(rect().height()-m_item->boundingRect().height()*m_item->scale())/2 );
// Set picture to center of the sceen
m_item->setPos(m_center);
}
void MainWindow::mousePressEvent(QMouseEvent *)
{
// Set picture to center of the sceen
m_item->setPos(m_center);
m_item->setScale(0.5);
}
void MainWindow::createMenu()
{
QMenu* menu = this->menuBar()->addMenu("Animate");
menu->addAction("Position", this, SLOT(animatePos()));
menu->addAction("Scale", this, SLOT(animateScale()));
}
void MainWindow::animatePos()
{
// Animate picture position
m_item->animatePosition(m_item->pos(),QPoint(0-this->rect().size().width(),0));
}
void MainWindow::animateScale()
{
m_item->animateScale(1.0,2.0);
}
To copy to clipboard, switch view to plain text mode
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui>
{
Q_OBJECT
// For Property animation:
// Change picture position
Q_PROPERTY(QPointF pos READ pos WRITE setPos
)
// Scale picture
Q_PROPERTY(qreal scale READ scale WRITE setScale)
public:
enum
{
PictureItemType = UserType + 1
};
public:
PictureItem
(QSizeF size,
const QPixmap
& pixmap
= 0,
QObject* parent
= 0);
~PictureItem();
int type() const
{
return PictureItemType;
}
// Animate position of this class
void animatePosition(const QPointF& start, const QPointF& end);
// Animate scale for this class
void animateScale(qreal start, qreal end);
public slots:
void animationFinished();
private:
};
{
Q_OBJECT
public:
~MainWindow();
void createMenu();
public slots:
void animatePos();
void animateScale();
private:
PictureItem* m_item;
};
#endif // MAINWINDOW_H
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui>
class PictureItem: public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
// For Property animation:
// Change picture position
Q_PROPERTY(QPointF pos READ pos WRITE setPos)
// Scale picture
Q_PROPERTY(qreal scale READ scale WRITE setScale)
public:
enum
{
PictureItemType = UserType + 1
};
public:
PictureItem(QSizeF size, const QPixmap& pixmap = 0, QObject* parent = 0);
~PictureItem();
QRectF boundingRect() const;
int type() const
{
return PictureItemType;
}
// Animate position of this class
void animatePosition(const QPointF& start, const QPointF& end);
// Animate scale for this class
void animateScale(qreal start, qreal end);
public slots:
void animationFinished();
private:
QSizeF m_size;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
void resizeEvent(QResizeEvent *);
void mousePressEvent(QMouseEvent *);
void createMenu();
public slots:
void animatePos();
void animateScale();
private:
QGraphicsScene* m_scene;
QGraphicsView* m_view;
PictureItem* m_item;
QPointF m_center;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
Bookmarks