Sourabh
Sourabh

Reputation: 765

Applying QGraphicsDropShadowEffect more than once crashes the app

I have to apply shadow effect on QPushButton based on some condition. I have to remove shadow effect, if some condition is false and add it again, if the condition becomes true. I am trying to use the following code, but the program crashes.

QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect();
effect->setBlurRadius(1);
effect->setOffset(2,2);
ui->btnAdd->setGraphicsEffect(effect);
ui->btnAdd->setGraphicsEffect(NULL); //remove effect
ui->btnAdd->setGraphicsEffect(effect); //add again

What is wrong with this code? Is there any other method to do it?

Upvotes: 3

Views: 1171

Answers (1)

Nejat
Nejat

Reputation: 32665

You can read in the Qt documentation about setGraphicsEffect :

Sets effect as the widget's effect. If there already is an effect installed on this widget, QWidget will delete the existing effect before installing the new effect.

So when this line is run :

ui->btnAdd->setGraphicsEffect(NULL); //remove effect

effect actually gets deleted. So you should make a new instance of the effect each time you want to set it.

Upvotes: 3

Related Questions