PDA

View Full Version : Qt Signal only emit once? (Signal & Slot problem)



SlaynX
3rd May 2011, 16:54
I was testing on Signal and slot based on QGraphicsScene and also QPropertyAnimation.
Basically I was trying to create a continuous loop of animation using signal and slot.
I present my code in a simple pseudo code:



class PictureItem
{
void animate()
{
QPropertyAnimation ani = new QPropertyAnimation
//animation settings eg. duration, setStartValue, setEndValue
//destroys the animation QAbstractAnimation::DeleteWhenStopped
connect(ani, SIGNAL(destroyed()),this, SLOT(deleteLater))
}
}

class Widget
{
constructor
{
sets the QGraphicsScene
sets the QGraphicsView
initPic()
connect(item, SIGNAL(destroyed()),this,SLOT(ReCreate())
}
void initPic()
{
PictureItem item = new PictureItem(....)
item->animate();
}
void ReCreate()
{
initPic()
}
}

So when i run it, the animation will play once which is based on my first initPic() and will play once more because of the signal and slot. However for the third time the animation doesn't play anymore. I perform a lot of qDebug testing and found out that the SIGNAL in the void animate() function doesn't emit anymore for the 2nd time. Is it because Qt signal can only be emitted once?

Lykurg
3rd May 2011, 17:03
Your code does not work because
connect(item, SIGNAL(destroyed()),this,SLOT(ReCreate())is at the wrong place. You have to put in inside the initPic method.

SlaynX
3rd May 2011, 20:22
Thanks a lot. It works now.