Results 1 to 2 of 2

Thread: QGraphicsObject motion animations is distorting object

  1. #1
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsObject motion animations is distorting object

    I have created a wheel shape as QGraphicsObject and wanted to move along X-Axis using QPropertyAnimation. When I move this object, it gets distorted as attached in picture. My code is as follows.


    Qt Code:
    1. #ifndef SHAPE_H
    2. #define SHAPE_H
    3.  
    4. #include <QGraphicsObject>
    5. #include <QtGui>
    6.  
    7. class shape : public QGraphicsObject
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit shape(QGraphicsItem *parent = 0);
    12. QRectF boundingRect() const{ return QRectF(0,0,150,150);}
    13. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    14.  
    15. signals:
    16.  
    17. public slots:
    18.  
    19. };
    20.  
    21. #endif // SHAPE_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "shape.h"
    2.  
    3. shape::shape(QGraphicsItem *parent) :
    4. QGraphicsObject(parent)
    5. {
    6. }
    7.  
    8. void shape::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    9. {
    10. painter->setRenderHint(QPainter::Antialiasing);
    11. painter->setPen(QPen(Qt::red,20));
    12. painter->setBrush(Qt::transparent);
    13. painter->drawEllipse(0,0,100,100);
    14.  
    15. painter->drawLine(50,0,50,100);
    16. painter->drawLine(0,50,100,50);
    17.  
    18. painter->drawEllipse(40,40,20,20);
    19. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef WHEEL_H
    2. #define WHEEL_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtGui>
    6. #include "shape.h"
    7.  
    8. namespace Ui {
    9. class wheel;
    10. }
    11.  
    12. class wheel : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit wheel(QWidget *parent = 0);
    18. ~wheel();
    19.  
    20. private:
    21. Ui::wheel *ui;
    22. };
    23.  
    24. #endif // WHEEL_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "wheel.h"
    2. #include "ui_wheel.h"
    3.  
    4. wheel::wheel(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::wheel)
    7. {
    8. ui->setupUi(this);
    9. scene = new QGraphicsScene;
    10. scene->setSceneRect(0,0,1,1);
    11. ui->graphicsView->setCacheMode(QGraphicsView::CacheBackground);
    12. ui->graphicsView->setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
    13. ui->graphicsView->setScene(scene);
    14. shape *s = new shape;
    15. s->setPos(100,100);
    16. scene->addItem(s);
    17. QPropertyAnimation *anim1 = new QPropertyAnimation(s,"pos");
    18. anim1->setDuration(5000);
    19. anim1->setEndValue(QPointF(300,100));
    20. anim1->start();
    21. ui->graphicsView->update();
    22. }
    23.  
    24. wheel::~wheel()
    25. {
    26. delete ui;
    27. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "wheel.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. wheel w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QGraphicsObject motion animations is distorting object

    Try compensating for your pen size in shape::boundingRect(). See here.

Similar Threads

  1. protected QGraphicsObject
    By stefan in forum Qt Programming
    Replies: 6
    Last Post: 19th August 2011, 17:04
  2. Animations
    By hema in forum Newbie
    Replies: 2
    Last Post: 11th July 2011, 12:05
  3. Animations
    By hema in forum The GraphicsView Framework
    Replies: 0
    Last Post: 8th July 2011, 06:54
  4. Replies: 1
    Last Post: 24th January 2011, 02:42
  5. Replies: 0
    Last Post: 10th December 2010, 13:36

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.