PDA

View Full Version : Force the painting of a QGraphicsItem



fabietto
29th June 2007, 10:35
Hello guys,

I'd like to ask you a question about the Qt Graphics View Framework (Qt 4.3, MacOS X 10.4.10).

I created a custom class that inherits from QGraphicsItem, defined as follows:


#ifndef SIMTARGET_H
#define SIMTARGET_H

#include <QGraphicsItem>
#include <QString>

class Sim_Target : public QObject, public QGraphicsItem {

Q_OBJECT

public:

// Constructor
Sim_Target();

// itemChange() function
QVariant itemChange(GraphicsItemChange change, const QVariant & value);

// Shape
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

// Tooltip
QString str_target_tooltip;

private:
QColor color;

signals:
void dragged();

};

#endif

In a slot belonging to my application's main window, I create an instance of the object and I insert it into the scene:


// Create the target and add it to the scene
simulationTarget = new Sim_Target;
graphScene_Scene->addItem(simulationTarget);

then I perform some basic operations on the object, trying to move it:


simulationTarget->setPos(10,30);
for (int counter=1; counter<1000; counter++) {
simulationTarget->setPos(simulationTarget->x()+1, simulationTarget->y());
}

The problem is that, during the execution of the for-cycle, I cannot see the object moving. At the end of the for-cycle, of course, when the application's control returns to the event loop, I see the object occupying the expected position (ie, the movement was performed correctly).

I think I have to call some functions that force the continuos repainting of the object after each movement. But looking at the documentation I didn't understand which is the function I should call. Again, while the for-cycle is executing, I'd like to be able to control the mouse (for example to press the button that quits my application); instead, until the program doesn't reach the event loop, I can only see my mouse cursor as a spinning beachball (the Mac's equivalent for the Windows' hourglass).

Could someone offer me some suggests, please? :-(

wysota
29th June 2007, 10:57
A bad solution is to call QApplication::processEvents() in the loop. A good solution is to have a QTimeLine object that controls the animation which is performed from within some slot.

fabietto
2nd July 2007, 12:26
Hello Wysota,

sorry for my late reply. Anyway I appreciated very much your hints. I'm now studying the documentation to figure out how to use QTimeLine with QGraphicsItemAnimation.

Thanks a lot,
Fabio

wysota
2nd July 2007, 21:28
There is an example of using QGraphicsItemAnimation in its docs.