1

I would like to animate the size of the QPushButton icon, because when i try to animate whole QPushButton object size, icon is not resizing at all. It seems theres no property "setScaledContents" on QPushButton icon. I tried to use this code for icon in my custom QPushButton method (on hover):

QIcon *icon = new QIcon(this->icon());
QPropertyAnimation *animation = new QPropertyAnimation((QObject*)icon, "size");
animation->setDuration(1000);
QSize test = this->size();
animation->setStartValue(test);

animation->setEndValue(QSize(test.width()+100,test.height()+100));

animation->start();

But i receive an error "segmentation failed" on this code. Is there any other way i can animate my QPushButton icon?

SirLanceloaaat
  • 213
  • 9
  • 18
  • 1
    `QIcon` doesn't inherit from `QObject`, so this type cast: `(QObject*)icon` is dangerous and wrong. – Chris Jul 24 '13 at 16:07
  • 1
    QPushButton has iconSize property. Why not animate it instead of animating QIcon? – Kamil Klimek Jul 24 '13 at 16:11
  • I tried `QPropertyAnimation *animation = new QPropertyAnimation(this, "iconSize"); animation->setDuration(500); // QSize test = this->size(); animation->setStartValue(QSize(1024,1024)); animation->setEndValue(QSize(512,512)); animation->start(); ` but it doesn't work – SirLanceloaaat Jul 24 '13 at 17:56
  • Changing to "iconSize" worked for me just fine... I did not bother subclassing QPushButton and I made sure to assign an icon to QPushButton (arbitrary PNG). Try it out with just "size" first – Son-Huy Pham Jul 24 '13 at 18:05

1 Answers1

2

What you need to do is instead pass in a valid reference to a QObject (per Chris' comment above)

Your code works fine if I simply replace the parameter passed into QPropertyAnimation:

// ui->pushButton is a QPushButton*
QPropertyAnimation *animation = new QPropertyAnimation(ui->pushButton, "size");
animation->setDuration(1000);
QSize test = this->size();
animation->setStartValue(test);
animation->setEndValue(QSize(test.width()+100,test.height()+100));
animation->start();

I'm going to assume that you're subclass-ing QPushButton (explains this->icon())... perhaps you can try to access it directly instead? Though I'm going to guess that it's privately owned by the base/parent class.

Son-Huy Pham
  • 1,899
  • 18
  • 19
  • Hello, yes im subclassing the QPushButton but im doing this to get the acces to hover property – SirLanceloaaat Jul 24 '13 at 17:39
  • I already tried to animate QPushButton itself but the icon doesn't resize with it. (no scaled contents property). I need a slow animation effect of changing size on image itself. – SirLanceloaaat Jul 24 '13 at 17:43