Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a shadow to a Frameless window in Qt

I'm trying to create a Frameless window that has a shadow under it.When we create a borderless window with Qt::FramelessWindowHint flag it doesn't contain a shadow. But we can put shadows to a child widgets easy by creating a QGraphicsDropShadowEffect object and then pass it to the widget through setGraphicsEffect function. But this doesn't seem to work for QMainWindow. Please help me to put shadow to a frameless window in Qt...

like image 595
Thusitha Avatar asked Sep 10 '12 08:09

Thusitha


2 Answers

You can do it using this simple hack:


Add a "QWidget" (say widget) to the MainWindow and move everything that's on the MainWindow to the widget. Then do this:

setAttribute(Qt::WA_TranslucentBackground); //enable MainWindow to be transparent

QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
effect->setBlurRadius(5);
ui->widget->setGraphicsEffect(effect);

This seems to work for me. See:

illusion of a main window having a shadow

like image 188
zeFree Avatar answered Nov 05 '22 17:11

zeFree


I followed exactly the instructions that were given by zeFree (using Qt/C++) and my shadow was indeed click-through (using Elementary OS Freya - I don't know if there is a different behavior between it and Ubuntu, OX or Windows)... I can't imagine why yours wasn't.

The only difference is that I made both the parent window margins and the blur radius larger, and removed the shadow offset.

Here my snippet:

void addDialogShadow(QWidget *target) {
    target->window()->setAttribute(Qt::WA_TranslucentBackground);
    target->window()->layout()->setMargin(50);
    QGraphicsDropShadowEffect* ef = new QGraphicsDropShadowEffect;
    ef->setBlurRadius(50);
    ef->setOffset(0);
    target->setGraphicsEffect(ef);
}

EDIT: No, my windows were not clickthrough (I think I was asleep when I wrote that). Indeed even Qt::WA_TransparentForMouseEvent attribute in my QDialog instance doesn't work. Grinding information on the web, I've found making this work is much more difficult than I thought.

like image 22
Vinícius A. Jorge Avatar answered Nov 05 '22 16:11

Vinícius A. Jorge