PDA

View Full Version : Make image bigger on enterEvent, maller on leaveEvent



Globulus
10th August 2011, 12:25
Hello!

I want QLabel, that contains image, make image bigger on enterEvent,
and smaller on leaveEvent. (animate it by QPropertyAnimation and QStateMachine ). For this purposes I subclassed QLabel, just like this:




class MyLabel : public QLabel
{
Q_OBJECT

public:
MyLabel(QWidget * parent = 0);
~MyLabel();

protected slots:

virtual void enterEvent ( QEvent * event );
virtual void leaveEvent ( QEvent * event );
};

MyLabel::MyLabel(QWidget * parent) : QWidget(parent)
{
}

MyLabel::~MyLabel()
{
}

void MyLabel::enterEvent ( QEvent * event )
{
}

void MyLabel::leaveEvent ( QEvent * event )
{
}



But what next - have no idea...

Tried something like :




void MyLabel::enterEvent ( QEvent * event )
{

QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
animation->setDuration(600);
animation->setStartValue(QRect(this->geometry().x(), this->geometry().y(), 64, 64));
animation->setEndValue(QRect(this->geometry().x(), this->geometry().y(), 128, 128));
animation->start();
}


But that is not the idea, because this->geometry().x() returns 0....

qlands
10th August 2011, 13:38
Hi,

Try by adding the constructor


this->setScaledContents(true);


Also, do work with x and y because this is the position of the object in its parent and usually is 0,0

So you can:



void mylabel::enterEvent(QEvent *)
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
animation->setDuration(600);
animation->setStartValue(this->rect()); //Get the curren rectangle
QRect final;
final = this->rect(); //The final is = the current
final.setWidth(final.width()*4); //Make it bigger
final.setHeight(final.height()*4); //Make it bigger

animation->setEndValue(final); //Set the final
animation->start();
}


Hope this helps.
Carlos

Globulus
10th August 2011, 15:04
Thanks, Carlos, for your help,but something is wrong...

qDebug() << this->rect();

Debug out is :



QRect(0,0 101x101) //one time enterEvent
QRect(0,0 242x242) //second time enterEvent


It seems that, your code really made image bigger but,
position of the QLabel in its parent is 0,0.

So now, I need to get QLabel position on mainwindow from my MyLabel class.

Any ideas, I will be very grateful!

qlands
10th August 2011, 15:26
Yes, is re-sizes the label from 0,0, with it (top,left) where paint usually starts.

From what you said, the second time the rect is bigger that the first time... Did you make it smaller in the mouseLeave()?

If you want to re-size the image in the way that it looks as its blowing from the center, you may need to implement you own animation with a QTimer or QVariantAnimation and work out the position of the object in terms of x and Y when the object changes its size.

Globulus
10th August 2011, 15:38
If you want to re-size the image in the way that it looks as its blowing from the center, you may need to implement you own animation with a QTimer or QVariantAnimation and work out the position of the object in terms of x and Y when the object changes its size.

Yes, that's exactly what I want...