PDA

View Full Version : How to change the SIZE of a graphicsItem during animation(QGraphicsItemAnimation).



akash
29th August 2011, 13:21
HI,
I want to change the SIZE of a graphicsItem (eclipse) during its animation using QGraphicsItemAnimation.

The Eclipse is drawn from the below code:
---

QRectF Node::boundingRect() const
{
qreal adjust = 2;
return QRectF( -10 - adjust, -10 - adjust,
23 + adjust, 23 + adjust);
}

void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
painter->setBrush(Qt::red);
painter->setPen(QPen(Qt::red, 0));
painter->drawEllipse(-10, -10, 20, 20);
}
---
Eclipse(Node) of above size got created.

Now in QGraphicsItemAnimation i want to change it's size to (-20,-20,40,40).

Could you please help me in changing the size of the eclipse. what functions can be used.

thanks,
Akash

Rachol
29th August 2011, 13:35
just use member variables instead of hardcoded values. Write a setter method to set these variables whenever you need. Remember to call prepareGeometryChange() before changing these variables in the setter method.

akash
29th August 2011, 15:38
Hi,

Thanks for the suggestion, partial problem is resolved.
I have another query regarding this.
I have n no. of nodes(eclipses) created from the same boundingrect and paint function, but these nodes(eclipses) are placed at different points in a scene.

Q. i want to change the size of all nodes(eclipses) simultaneously (parallel animation) by some random values +/- their width and height.
is there any way to do paralel animation in QGraphicsItemAnimation.

Thanks in advance,
Akash

wysota
29th August 2011, 18:48
Why don't you use the animation framework instead of QGraphicsItemAnimation?

akash
30th August 2011, 08:27
Hi,
I have used QParallelAnimationGroup for parallel animation of n buttons. But i think QParallelAnimationGroup only supports QWIdgets and not graphicsitems. This was the reason i switched to QGraphicsItemAnimation to animate GraphicsItems.

Please let me know if there is any way to do parallelanimation for graphicsitems in QParallelAnimation/QGraphicsItemAnimation.
Or can we convert GraphicsItem to QWidget and use QParallelAnimationGroup to animate.

Correct me if i m leading in wrong direction.

Thanks,
Akash

wysota
30th August 2011, 08:52
I have used QParallelAnimationGroup for parallel animation of n buttons. But i think QParallelAnimationGroup only supports QWIdgets and not graphicsitems.
No, that's not true. Have a look at the "animated tiles" demo.

akash
31st August 2011, 07:54
Hi,

I have gone through the animated tiles example, thanks for the suggestion.
In this example the animation is applied on pos of a pixmap.
Now, if i want to change the size of this pixmap through animation, what QtProperty should be taken care of.

Below is the code change that i did to change the size of pixmap.

class Pixmap : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
// Q_PROPERTY(QPointF pos READ pos WRITE setPos)
Q_PROPERTY(QRectF geometry READ geometry WRITE setGeometry)

* i replaced pos with geometry.

After changing some code, below error occured.

Error: geometry/setGeometry not declared in scope.

My understanding:
setGeometry is not used in right place/not the right property that can change the size of pixmap/graphicitem.

Could you please let me know what code and where should i change the code to get the proper output.

Solution to this problem may solve my entire problem.


Thanks,

wysota
31st August 2011, 08:40
QGraphicsPixmapItem doesn't have "geometry" and "setGeometry" methods. You'll have to implement them if you want to have such a property. However I don't see how this would work since the pixmap is of fixed size. Maybe you want to scale and not resize the item? If so, then it will be easiest to use QGraphicsScale which already has appropriate properties defined.

akash
31st August 2011, 10:00
Hi,
Here is the main problem that i m trying to resolve.

Problem:
In a graph i have nodes and edges. Initially the size of nodes and edges are fixed. I have to change the size of nodes as per some random data from a file or from an array. The size of all nodes should get change simultaneously along with the size/length of edges connecting them to give proper animation effect.

To Solve this problem i used "elasticnodes" example and convert this moving node to static graph. i removed their calculate force function so that nodes should not move automatically after draging any node.

Currently i have nodes and edges well connected to each other, with the help of a mouse i can change the positon of nodes, connections dont get distorted after changing the positions and gives elastic effects.

I tried scaleing a node and it was working fine, but connected edges were not changing their length and connections were distorted. so, i planned to change the size of nodes and shift their positions to give proper effect of scaling and as per my understanding connected edges should/will change their length as per the node positions.

Road Blocks:
1. Unable to change the size of nodes.
2. Scaling the nodes distorts the connections.
3. Unable to do all above simultaneously/ parallelanimation on graphicitems.

Please guide me on this, so that i can step in right direction and reslove this issue.

thanks in advance for you attention,

~Akash

wysota
31st August 2011, 10:37
Then why are you using pixmap items?

Here is a small demo:

#include <QtGui>

class Item : public QGraphicsObject {
Q_OBJECT
Q_PROPERTY(qreal size READ size WRITE setSize)
public:
Item(QGraphicsItem *parent = 0) : QGraphicsObject(parent) {
setSize(10.0);
}
void setSize(qreal s) {
if(m_rect.width()==s) return;
m_rect = QRectF(-s/2,-s/2, s, s);
prepareGeometryChange();
}
qreal size() const { return m_rect.width(); }
QRectF boundingRect() const {
return m_rect;
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) {
painter->setBrush(Qt::red);
painter->drawEllipse(boundingRect());
}
private:
QRectF m_rect;
};

#include "main.moc"

int main(int argc, char **argv){
QApplication app(argc, argv);
qsrand(QDateTime::currentDateTime().toTime_t());
QGraphicsScene scene;
QGraphicsView view;
QParallelAnimationGroup *animGrp = new QParallelAnimationGroup(&scene);
animGrp->setLoopCount(-1);
for(int i=0;i<10;++i) {
Item *item = new Item;
scene.addItem(item);
item->setPos(qrand() % 400, qrand()%400);
QPropertyAnimation *anim = new QPropertyAnimation(item, "size", animGrp);
animGrp->addAnimation(anim);
anim->setStartValue(qrand()%30);
anim->setEndValue(40+qrand()%50);
anim->setEasingCurve(QEasingCurve::Type(qrand()%41));
anim->setDuration(250*(qrand()%10));
}

view.setScene(&scene);
view.show();
view.setRenderHint(QPainter::Antialiasing);
animGrp->start();
return app.exec();
}

akash
2nd September 2011, 10:41
Hi,
Thanks for the pointers.
Problem is resolved.
I used pixmap because i was trying to get solution of problem by changing the properties of a graphicsitem,and was trying to learn by doing hit and trial.
once again thanks for your help.


~akash