Hi, i want to scale an image and i already did it with help of it: http://wiki.forum.nokia.com/index.ph...s_item_scaling but the scaling is made from point(0,0) and i want that the scaling is made from the center of the image.
I try it for the last hours without success (i'm despairing :s)
I attached the project and the code are below:

Qt Code:
  1. //main.cpp
  2.  
  3. #include <QtGui/QApplication>
  4. #include "mainwindow.h"
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. QApplication a(argc, argv);
  9. MainWindow w;
  10. w.setFixedSize(240,320);
  11. //w.setMinimumSize(240,320);
  12. w.show();
  13. return a.exec();
  14. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. //mainwindow.cpp
  2.  
  3. #include "mainwindow.h"
  4. #include <QDebug>
  5. #include <QAction>
  6. #include <QFile>
  7.  
  8. bool zoomIn=false;
  9.  
  10. PictureItem::PictureItem(QSizeF size, const QPixmap& pixmap, QObject* parent) :
  11. QObject(parent), QGraphicsPixmapItem(pixmap)
  12. {
  13. m_size = size;
  14. setCacheMode(DeviceCoordinateCache);
  15. }
  16.  
  17. PictureItem::~PictureItem()
  18. {
  19. }
  20.  
  21. QRectF PictureItem::boundingRect() const
  22. {
  23. return QRectF(0, 0, m_size.width(), m_size.height());
  24. }
  25.  
  26. void PictureItem::animatePosition(const QPointF& start, const QPointF& end)
  27. {
  28. // Start animate this class
  29. QPropertyAnimation* anim = new QPropertyAnimation(this, "pos");
  30.  
  31. // 2 second duration animation
  32. anim->setDuration(500);
  33. // position to start animation
  34. anim->setStartValue(start);
  35. // end position of animation
  36. anim->setEndValue(end);
  37. // easing curve
  38. anim->setEasingCurve(QEasingCurve::OutQuad);
  39.  
  40. // Listen animation finished signal
  41. QObject::connect(anim, SIGNAL(finished()), this, SLOT(animationFinished()));
  42.  
  43. // Start animation and delete QPropertyAnimation class on the end
  44. anim->start(QAbstractAnimation::DeleteWhenStopped);
  45. }
  46.  
  47. void PictureItem::animateScale(qreal start, qreal end)
  48. {
  49. // Start animate this class
  50. QPropertyAnimation* anim = new QPropertyAnimation(this, "scale");
  51. anim->setDuration(1000);
  52. anim->setStartValue(start);
  53. anim->setEndValue(end);
  54. anim->setEasingCurve(QEasingCurve::InOutBack);
  55. QObject::connect(anim, SIGNAL(finished()), this, SLOT(animationFinished()));
  56.  
  57. anim->start(QAbstractAnimation::DeleteWhenStopped);
  58. }
  59.  
  60. void PictureItem::animationFinished()
  61. {
  62. qDebug()<<"Animation Finished!";
  63. }
  64.  
  65. MainWindow::MainWindow(QWidget *parent)
  66. : QMainWindow(parent)
  67. {
  68.  
  69. // Create QGraphicsScene and QGraphicsView
  70. m_scene = new QGraphicsScene(this);
  71. m_view = new QGraphicsView(m_scene,this);
  72. m_view->setCacheMode(QGraphicsView::CacheBackground);
  73. m_view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
  74. m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  75. m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  76.  
  77. // Create PictureItem for the animation
  78. QPixmap pixmap(":/qt1.jpg");
  79. m_item = new PictureItem(pixmap.size(),pixmap,this);
  80.  
  81. // Add PictureItem to center of the scene
  82. m_scene->addItem(m_item);
  83. m_item->setScale(0.5);
  84. createMenu();
  85.  
  86. this->setCentralWidget(m_view);
  87.  
  88. }
  89.  
  90. MainWindow::~MainWindow()
  91. {
  92. }
  93.  
  94. void MainWindow::resizeEvent(QResizeEvent *)
  95. {
  96. // Update scene rect
  97. m_scene->setSceneRect(m_view->rect());
  98.  
  99. // Calculate center point for the picture
  100. m_center = QPointF((rect().width()-m_item->boundingRect().width()*m_item->scale())/2,
  101. (rect().height()-m_item->boundingRect().height()*m_item->scale())/2 );
  102.  
  103. // Set picture to center of the sceen
  104. m_item->setPos(m_center);
  105. }
  106.  
  107. void MainWindow::mousePressEvent(QMouseEvent *)
  108. {
  109. // Set picture to center of the sceen
  110. m_item->setPos(m_center);
  111. m_item->setScale(0.5);
  112. }
  113.  
  114. void MainWindow::createMenu()
  115. {
  116. QMenu* menu = this->menuBar()->addMenu("Animate");
  117. menu->addAction("Position", this, SLOT(animatePos()));
  118. menu->addAction("Scale", this, SLOT(animateScale()));
  119. }
  120.  
  121. void MainWindow::animatePos()
  122. {
  123. // Animate picture position
  124. m_item->animatePosition(m_item->pos(),QPoint(0-this->rect().size().width(),0));
  125. }
  126.  
  127. void MainWindow::animateScale()
  128. {
  129. m_item->animateScale(1.0,2.0);
  130. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. //mainwindow.h
  2.  
  3. #ifndef MAINWINDOW_H
  4. #define MAINWINDOW_H
  5.  
  6. #include <QtGui>
  7.  
  8. class PictureItem: public QObject, public QGraphicsPixmapItem
  9. {
  10. Q_OBJECT
  11. // For Property animation:
  12.  
  13. // Change picture position
  14. Q_PROPERTY(QPointF pos READ pos WRITE setPos)
  15.  
  16. // Scale picture
  17. Q_PROPERTY(qreal scale READ scale WRITE setScale)
  18.  
  19. public:
  20. enum
  21. {
  22. PictureItemType = UserType + 1
  23. };
  24.  
  25. public:
  26. PictureItem(QSizeF size, const QPixmap& pixmap = 0, QObject* parent = 0);
  27. ~PictureItem();
  28.  
  29. QRectF boundingRect() const;
  30.  
  31. int type() const
  32. {
  33. return PictureItemType;
  34. }
  35.  
  36. // Animate position of this class
  37. void animatePosition(const QPointF& start, const QPointF& end);
  38.  
  39. // Animate scale for this class
  40. void animateScale(qreal start, qreal end);
  41.  
  42. public slots:
  43. void animationFinished();
  44.  
  45. private:
  46. QSizeF m_size;
  47. };
  48.  
  49. class MainWindow : public QMainWindow
  50. {
  51. Q_OBJECT
  52.  
  53. public:
  54. MainWindow(QWidget *parent = 0);
  55. ~MainWindow();
  56.  
  57. void resizeEvent(QResizeEvent *);
  58. void mousePressEvent(QMouseEvent *);
  59. void createMenu();
  60.  
  61. public slots:
  62. void animatePos();
  63. void animateScale();
  64.  
  65. private:
  66. QGraphicsScene* m_scene;
  67. QGraphicsView* m_view;
  68. PictureItem* m_item;
  69. QPointF m_center;
  70. };
  71.  
  72. #endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode 

teste_anim.zip