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?

4

1 回答 1

2

您需要做的是传入对QObject 的有效引用(根据克里斯上面的评论)

如果我简单地替换传递给 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 进行子类化(解释 this->icon())...也许您可以尝试直接访问它?虽然我猜它是由基/父类私有的。

于 2013-07-24T16:06:11.900 回答