Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable setGraphicsEffect inheritance in Qt

How to disable setGraphicsEffect inheritance to children in Qt?

For example, imagine my project has this hierarchy:

enter image description here

When I apply a shadow with setGraphicsEffect to the frame the line edit will inherit that shadow effect, and I don't want that. This is what I'am getting:

enter image description here

As you can see, there's a shadow around the letters of the line edit. Any idea how to fix this?

One more thing.. visibly in the image above the MainWindow has some flags like Qt::FramelessWindowHint and Qt::WA_TranslucentBackground so the frame is supposed to be the "window", and the frame automatically resizes when I resize the MainWindow so I can't change that hierarchy.

As requested by Timusan here is the code of the shadow: Qt: shadow around window

To apply it to the frame:

CustomShadowEffect *shadow = new CustomShadowEffect();    
shadow->setColor(c);  
shadow->setDistance(scale);  
shadow->setBlurRadius(blur_radius);  
ui->frame->setGraphicsEffect(shadow);  

UPDATE

layout()->addWidget(ui->lineEdit);  
like image 330
ComfortableChair Avatar asked Nov 17 '25 23:11

ComfortableChair


1 Answers

Documentation of QGraphicsItem::setGraphicsEffect states that

Note: This function will apply the effect on itself and all its children.

thus the behaivor you are expecting is correct.

What I'd suggest is overriding the graphics item on the QLineEdit explicitly by calling either setGraphicsEffect(NULL) or by getting the default text effect of the line edit and then setting it back after your custom effect on the frame has been applied. I'm not sure if the line edit has a default effect or not, thus not sure if the NULL approach will do.

UPDATE

As stated by the OP the proposed way of overriding the existing effect by calling QGraphicsItem::setGraphicsEffect with null does not work. So given that I see 2 further possibilies:

  • Don't make the QLineEdit a child of the frame (not sure if that is possible, you mentioned that frame cannot be unbound from the window but nothing about frame to line edit relationship constraints).
  • Since you already have subclassed the graphics effect with your CustomShadowEffect you could tweak the draw() method to ignore calls with QPainter that belongs to the line edit. What you could do is keep a list of ignored QPaintDevice instances in the effect - QWidget inherits QPaintDevice and you can query the QPaintDevice pointer from the QPainter via QPainter::device, so you can simply add the widgets to your internal list and then in the draw method compare the QPaintDevice of the incoming QPainter to the ignored ones.
like image 70
Rudolfs Bundulis Avatar answered Nov 19 '25 15:11

Rudolfs Bundulis