Qt Animate QPushButton图标

时间:2013-07-24 15:50:41

标签: c++ qt animation qpushbutton qicon

我想设置QPushButton图标的大小动画,因为当我尝试为整个QPushButton对象大小设置动画时,图标根本没有调整大小。似乎在QPushButton图标上没有属性“setScaledContents”。我尝试在我的自定义QPushButton方法中使用此代码作为图标(在悬停时):

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();

但我在此代码上收到错误“细分失败”。有没有其他方法可以动画我的QPushButton图标?

1 个答案:

答案 0 :(得分:1)

您需要做的是传递对QObject (per Chris' comment above)

的有效引用

如果我只是替换传递给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();

我将假设你是QPushButton的子类(解释这个 - > icon())...也许你可以尝试直接访问它?虽然我猜它是由基础/父类私有的。