Results 1 to 5 of 5

Thread: Make image bigger on enterEvent, maller on leaveEvent

  1. #1
    Join Date
    Aug 2011
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question Make image bigger on enterEvent, maller on leaveEvent

    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:

    Qt Code:
    1. class MyLabel : public QLabel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyLabel(QWidget * parent = 0);
    7. ~MyLabel();
    8.  
    9. protected slots:
    10.  
    11. virtual void enterEvent ( QEvent * event );
    12. virtual void leaveEvent ( QEvent * event );
    13. };
    14.  
    15. MyLabel::MyLabel(QWidget * parent) : QWidget(parent)
    16. {
    17. }
    18.  
    19. MyLabel::~MyLabel()
    20. {
    21. }
    22.  
    23. void MyLabel::enterEvent ( QEvent * event )
    24. {
    25. }
    26.  
    27. void MyLabel::leaveEvent ( QEvent * event )
    28. {
    29. }
    To copy to clipboard, switch view to plain text mode 

    But what next - have no idea...

    Tried something like :

    Qt Code:
    1. void MyLabel::enterEvent ( QEvent * event )
    2. {
    3.  
    4. QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
    5. animation->setDuration(600);
    6. animation->setStartValue(QRect(this->geometry().x(), this->geometry().y(), 64, 64));
    7. animation->setEndValue(QRect(this->geometry().x(), this->geometry().y(), 128, 128));
    8. animation->start();
    9. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Make image bigger on enterEvent, maller on leaveEvent

    Hi,

    Try by adding the constructor
    Qt Code:
    1. this->setScaledContents(true);
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. void mylabel::enterEvent(QEvent *)
    2. {
    3. QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
    4. animation->setDuration(600);
    5. animation->setStartValue(this->rect()); //Get the curren rectangle
    6. QRect final;
    7. final = this->rect(); //The final is = the current
    8. final.setWidth(final.width()*4); //Make it bigger
    9. final.setHeight(final.height()*4); //Make it bigger
    10.  
    11. animation->setEndValue(final); //Set the final
    12. animation->start();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Hope this helps.
    Carlos

  3. #3
    Join Date
    Aug 2011
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Make image bigger on enterEvent, maller on leaveEvent

    Thanks, Carlos, for your help,but something is wrong...

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

    Debug out is :

    Qt Code:
    1. QRect(0,0 101x101) //one time enterEvent
    2. QRect(0,0 242x242) //second time enterEvent
    To copy to clipboard, switch view to plain text mode 

    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!

  4. #4
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Make image bigger on enterEvent, maller on leaveEvent

    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.

  5. #5
    Join Date
    Aug 2011
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Make image bigger on enterEvent, maller on leaveEvent

    Quote Originally Posted by qlands View Post
    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...

Similar Threads

  1. Make image bigger, when the mouse is over QLabel
    By Globulus in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2011, 09:39
  2. How to make dropdown button bigger
    By Vadi in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2008, 23:54
  3. Replies: 7
    Last Post: 27th June 2007, 09:34
  4. enterEvent, leaveEvent, setWindowFlags
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2007, 09:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.