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?
Re: Moving an item in QGraphicsScene
Quote:
Originally Posted by
prosass
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
Code:
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?
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:
Code:
#ifndef CHIP_H
#define CHIP_H
#include <QtGui/QColor>
#include <QtGui/QGraphicsItem>
#include <QObject>
{
Q_OBJECT
public:
Chip
(const QColor &color,
int x,
int y,
const QString &tipo,
const qreal
&ancho,
const qreal
&alto,
const QPixmap &pixmap
);
protected:
[/B]
private:
int x, y;
qreal ancho;
qreal alto;
QList<QPointF> stuff;
};
And in the .cpp file I have reimplemented the timer event:
Code:
#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);
}
...
{
...
}
Now my problem is I get the error:
Code:
:: === 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?
Re: Moving an item in QGraphicsScene
QGraphicsItem is not a QObject. You would have to use multiple inheritance:
Anyway, if you plan to construct any kind of animation, I suggest using QGraphicsItemAnimation.
Re: Moving an item in QGraphicsScene
Thanks jpn, that solved the problem!