PDA

View Full Version : Moving an item in QGraphicsScene



prosass
26th March 2007, 17:14
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?

thawkins
26th March 2007, 19:29
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



QTimer qSceneUpdateTimer;
QGraphicsSene qMyScene; // some scene you will load up with images

// connecting the timer to your scene
connect(&qSceneUpdateTimer,SIGNAL(timeout()),&qMyScene,SLOT(advance()));

// add in whatever QGraphicsItem derived objects into your scene you want
...
...
// starting the 1 second timer to advance the objects
qSceneUpdateTimer.start(1000);


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?

prosass
28th March 2007, 13:08
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:



#ifndef CHIP_H
#define CHIP_H

#include <QtGui/QColor>
#include <QtGui/QGraphicsItem>
#include <QObject>

class Chip : public QGraphicsItem
{
Q_OBJECT

public:
Chip(const QColor &color, int x, int y, const QString &tipo, const qreal &ancho, const qreal &alto, const QPixmap &pixmap);

QRectF boundingRect() const;
QPainterPath shape() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget);

protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void timerEvent(QTimerEvent *event);
[/B]
private:
int x, y;
QColor color;
QString tipo;
QPixmap pixmap;
qreal ancho;
qreal alto;
QList<QPointF> stuff;
};

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



#include "chip.h"
#include "loc.h"

#include <QtGui>
#include <QtSql>


loc_position_t loc_pos;

Chip::Chip(const QColor &color, int x, int y, const QString &tipo, const qreal &ancho, const qreal &alto, const QPixmap &pixmap)
{
this->x = x;
this->y = y;
this->color = color;
this->tipo = tipo;
this->ancho = ancho;
this->alto = alto;
this->pixmap = pixmap;

setZValue(1);

setFlags(ItemIsSelectable);
setAcceptsHoverEvents(true);
startTimer(5000);

}

...

void Chip::timerEvent(QTimerEvent *)
{
...
}


Now my problem is I get the error:



:: === chip2, Debug ===
chip.cpp:24: error: `startTimer' undeclared (first use this function)

...



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?

jpn
28th March 2007, 13:18
QGraphicsItem is not a QObject. You would have to use multiple inheritance:


class Chip : public QObject, public QGraphicsItem // QObject must be listed first
{
...
};

Anyway, if you plan to construct any kind of animation, I suggest using QGraphicsItemAnimation.

prosass
28th March 2007, 14:21
Thanks jpn, that solved the problem!