Results 1 to 3 of 3

Thread: Scale Image centered

  1. #1
    Join Date
    Mar 2010
    Posts
    56
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Scale Image centered

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Scale Image centered


  3. #3
    Join Date
    Mar 2010
    Posts
    56
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Scale Image centered

    Quote Originally Posted by Lykurg View Post
    i already tested that but it change the position of image :s

Similar Threads

  1. Scale an image and assign it to a QPushButton
    By harmodrew in forum Newbie
    Replies: 2
    Last Post: 10th August 2010, 12:46
  2. how to scale a picture/image?
    By savaliya_ambani in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 26th May 2010, 13:51
  3. Bar Chart centered on Labels
    By liversedge in forum Qwt
    Replies: 1
    Last Post: 30th January 2010, 20:57
  4. Why QFileDialog::getOpenFileName is not centered?
    By ricardo in forum Qt Programming
    Replies: 20
    Last Post: 31st August 2009, 00:08
  5. centered text label
    By djconnel in forum Qwt
    Replies: 5
    Last Post: 28th April 2009, 18:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.