Results 1 to 3 of 3

Thread: Moving a QGraphicsItem by using QPropertyAnimation

  1. #1
    Join Date
    Mar 2015
    Posts
    8
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Moving a QGraphicsItem by using QPropertyAnimation

    I am planning to move a circle in the graphics view but it doesn't work. Here is my source code:

    circle.h
    Qt Code:
    1. #ifndef CIRCLE_H
    2. #define CIRCLE_H
    3. #include <QGraphicsItem>
    4. #include <QPainter>
    5.  
    6. class Circle : public QObject, public QGraphicsItem
    7. {
    8. Q_OBJECT
    9. Q_PROPERTY(QPointF pos READ pos WRITE setPos)
    10. public:
    11. Circle();
    12. ~Circle();
    13. QRectF boundingRect() const;
    14. protected:
    15. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    16. };
    17.  
    18. #endif // CIRCLE_H
    To copy to clipboard, switch view to plain text mode 
    dialog.h
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QtGui>
    6. #include <QPropertyAnimation>
    7. #include "circle.h"
    8.  
    9. namespace Ui {
    10. class Dialog;
    11. }
    12.  
    13. class Dialog : public QDialog
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit Dialog(QWidget *parent = 0);
    19. ~Dialog();
    20.  
    21. private:
    22. Ui::Dialog *ui;
    23. Circle *circle;
    24. QPropertyAnimation *animation;
    25. };
    26.  
    27. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 
    circle.cpp
    Qt Code:
    1. #include "circle.h"
    2.  
    3. Circle::Circle()
    4. {
    5.  
    6. }
    7.  
    8. Circle::~Circle()
    9. {
    10.  
    11. }
    12.  
    13. QRectF Circle::boundingRect() const
    14. {
    15. return QRectF(20, 100, 50, 50);
    16. }
    17.  
    18. void Circle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    19. {
    20. QRectF rect = boundingRect();
    21. QPen pen(Qt::NoPen);
    22. QBrush brush(Qt::blue);
    23. brush.setStyle(Qt::SolidPattern);
    24.  
    25. painter->setPen(pen);
    26. painter->setBrush(brush);
    27. painter->drawEllipse(rect);
    28. }
    To copy to clipboard, switch view to plain text mode 
    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. Dialog::Dialog(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::Dialog)
    7. {
    8. ui->setupUi(this);
    9.  
    10. scene = new QGraphicsScene(this);
    11. ui->graphicsView->setScene(scene);
    12.  
    13. circle = new Circle;
    14. scene->addItem(circle);
    15.  
    16. animation = new QPropertyAnimation(circle, "geometry", NULL);
    17. animation->setDuration(5000);
    18. animation->setEndValue(QRect(500, 100, 150, 150));
    19.  
    20. animation->start();
    21. }
    22.  
    23. Dialog::~Dialog()
    24. {
    25. delete ui;
    26. }
    To copy to clipboard, switch view to plain text mode 

    Can anyone please tell me how to solve this thing? Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Moving a QGraphicsItem by using QPropertyAnimation

    Your property is called "pos" .
    You are trying to animate non existant property "geometry"

    Cheers,
    _

  3. #3
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: Moving a QGraphicsItem by using QPropertyAnimation

    Neither QObject nor QGraphicsItem seem to have the 'geometry' property you're assigning to that animation controller. So it's not defined anywhere, and this is probably why nothing happens.

    The documentation specifies the properties of QObject-based classes:
    http://doc.qt.io/qt-5/qobject.html
    http://doc.qt.io/qt-5/qgraphicsitem.html

    So in your case you will probably have to write something like this:
    Qt Code:
    1. Q_PROPERTY(QRectF geometry READ geometry WRITE setGeometry)
    2.  
    3. void setGeometry( QRectF& newRect )
    4. {
    5. geometry = newRect // Copy.
    6. setPos( newRect.topLeft() );
    7. }
    8.  
    9. QRectF boundingRect()
    10. {
    11. return geometry;
    12. }
    To copy to clipboard, switch view to plain text mode 
    Also, make sure you're only using QRectF values (for example in the 'setEndValue' function).

Similar Threads

  1. Moving of QGraphicsItem
    By Erlendhg in forum Qt Programming
    Replies: 17
    Last Post: 5th November 2013, 15:04
  2. Replies: 21
    Last Post: 18th June 2013, 10:25
  3. resizing and moving a QGraphicsItem
    By xtraction in forum Qt Programming
    Replies: 1
    Last Post: 21st February 2012, 10:19
  4. Moving child QGraphicsItem
    By roband915 in forum Qt Programming
    Replies: 0
    Last Post: 27th February 2011, 12:19
  5. Moving QGraphicsItem with mouse
    By BrainFreeze in forum Qt Programming
    Replies: 9
    Last Post: 11th February 2011, 00:48

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.