Results 1 to 5 of 5

Thread: Moving an item in QGraphicsScene

  1. #1
    Join Date
    Feb 2007
    Posts
    15
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Question Moving an item in QGraphicsScene

    Hello everyone again,
    I have several items in a scene. The position of those items is retrieved through the serial port and stored in a database. That is no problem. Now I want to update the scene reflecting the changes in the position of the items every second.

    What would be the best approach to accomplish that?

  2. #2
    Join Date
    Jan 2006
    Location
    Florida
    Posts
    24
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Moving an item in QGraphicsScene

    Quote Originally Posted by prosass View Post
    Hello everyone again,
    I have several items in a scene. The position of those items is retrieved through the serial port and stored in a database. That is no problem. Now I want to update the scene reflecting the changes in the position of the items every second.

    What would be the best approach to accomplish that?
    seems like all you need to do is set up a QTimer to fire every second and call the advance() slot of your QGraphicsScene interface

    Qt Code:
    1. QTimer qSceneUpdateTimer;
    2. QGraphicsSene qMyScene; // some scene you will load up with images
    3.  
    4. // connecting the timer to your scene
    5. connect(&qSceneUpdateTimer,SIGNAL(timeout()),&qMyScene,SLOT(advance()));
    6.  
    7. // add in whatever QGraphicsItem derived objects into your scene you want
    8. ...
    9. ...
    10. // starting the 1 second timer to advance the objects
    11. qSceneUpdateTimer.start(1000);
    To copy to clipboard, switch view to plain text mode 

    The only hitch with this is that you need some way of giving the actual animation "instructions" to the individual graphics items. If they are set paths or transformations that have regular intervals (i.e. change every second), all you need to do is reimplement the objects void QGraphicsItem::advance ( int phase ) method, making sure that you perform the changes when phase == 1

    Is that what you were looking for?

  3. #3
    Join Date
    Feb 2007
    Posts
    15
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Moving an item in QGraphicsScene

    Thanks thawkins, but I think it´s not exactly what I need.

    I have followed the mice colliding example and I´ve created a timer event:

    The .h file:

    Qt Code:
    1. #ifndef CHIP_H
    2. #define CHIP_H
    3.  
    4. #include <QtGui/QColor>
    5. #include <QtGui/QGraphicsItem>
    6. #include <QObject>
    7.  
    8. class Chip : public QGraphicsItem
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. Chip(const QColor &color, int x, int y, const QString &tipo, const qreal &ancho, const qreal &alto, const QPixmap &pixmap);
    14.  
    15. QRectF boundingRect() const;
    16. QPainterPath shape() const;
    17. void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget);
    18.  
    19. protected:
    20. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    21. void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    22. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    23. void timerEvent(QTimerEvent *event);
    24. [/B]
    25. private:
    26. int x, y;
    27. QColor color;
    28. QString tipo;
    29. QPixmap pixmap;
    30. qreal ancho;
    31. qreal alto;
    32. QList<QPointF> stuff;
    33. };
    To copy to clipboard, switch view to plain text mode 

    And in the .cpp file I have reimplemented the timer event:

    Qt Code:
    1. #include "chip.h"
    2. #include "loc.h"
    3.  
    4. #include <QtGui>
    5. #include <QtSql>
    6.  
    7.  
    8. loc_position_t loc_pos;
    9.  
    10. Chip::Chip(const QColor &color, int x, int y, const QString &tipo, const qreal &ancho, const qreal &alto, const QPixmap &pixmap)
    11. {
    12. this->x = x;
    13. this->y = y;
    14. this->color = color;
    15. this->tipo = tipo;
    16. this->ancho = ancho;
    17. this->alto = alto;
    18. this->pixmap = pixmap;
    19.  
    20. setZValue(1);
    21.  
    22. setFlags(ItemIsSelectable);
    23. setAcceptsHoverEvents(true);
    24. startTimer(5000);
    25.  
    26. }
    27.  
    28. ...
    29.  
    30. void Chip::timerEvent(QTimerEvent *)
    31. {
    32. ...
    33. }
    To copy to clipboard, switch view to plain text mode 

    Now my problem is I get the error:

    Qt Code:
    1. :: === chip2, Debug ===
    2. chip.cpp:24: error: `startTimer' undeclared (first use this function)
    3.  
    4. ...
    To copy to clipboard, switch view to plain text mode 

    Why is that? I have rerun qmake and the Q_OBJECT macro is the .h file. I cannot find why the error. Can anyone help?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Moving an item in QGraphicsScene

    QGraphicsItem is not a QObject. You would have to use multiple inheritance:
    Qt Code:
    1. class Chip : public QObject, public QGraphicsItem // QObject must be listed first
    2. {
    3. ...
    4. };
    To copy to clipboard, switch view to plain text mode 
    Anyway, if you plan to construct any kind of animation, I suggest using QGraphicsItemAnimation.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    prosass (28th March 2007)

  6. #5
    Join Date
    Feb 2007
    Posts
    15
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Moving an item in QGraphicsScene

    Thanks jpn, that solved the problem!

Similar Threads

  1. QWidget item in QGraphicsScene
    By IUnknown in forum Qt Programming
    Replies: 4
    Last Post: 4th November 2006, 00:05
  2. QTableWidget item checkable and combo?
    By darpan in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2006, 07:12
  3. what item QCanvasItemList is holding..
    By Kapil in forum Newbie
    Replies: 17
    Last Post: 25th April 2006, 14:57
  4. moving Qlistview items
    By :db:sStrong in forum Qt Programming
    Replies: 0
    Last Post: 21st February 2006, 12:25
  5. how change the QListBox item position by pixel
    By roy_skyx in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2006, 01:34

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.