How to put a shadow to a Frameless window in Qt
Asked Answered
F

2

13

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...

Fulbert answered 10/9, 2012 at 8:21 Comment(0)
B
15

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

Befriend answered 2/4, 2013 at 6:13 Comment(11)
how did you create those graphics ?Transvestite
It's been almost an year so I don't remember exactly. Perhaps I used PicPick (picpick.org/en) for that -- took screenshot and did a little editing. It's a nice handy tool for taking screenshots and performing simple image manipulations. : )Befriend
I just tryed implementing this with PySide. Looks pretty neat but there are some big problems: 1st: the shadow is not ClickTrough! You are still clicking the window if you click the shadow :( 2nd: The shadow is part of the window. Maximizing it leaves a shadow gap (in case of really maximizing you could probably catch that and deactivate the shadow border) If you maximize horizontally or vertically only this is a little weirder... Darn. There must be a way of telling windows to use system shadow... The EA Origin game tool seems to be using this method though ^. I'd just like a bigger shadow ;]Hydromedusa
Thanks for this hack, but I got a problem when use the transparent stuff in this senario: bugreports.qt.io/browse/QTBUG-63199, any ideas? Thanks.Sarthe
Hi @BillHoo I'll look into this, but it will be easier if you can share your code (or simple dummy code which reproduces this issue) with me. You can paste it here (codeshare.io/5eMzD4) OR any other way you want.Befriend
@zeFree Thanks for reply, the demo code already put into your link. You can try and see what will happen if we enabled the WA_TransluentBackground feature.Sarthe
Thanks! I'll look into it and get back to you! And if I fix/resolve it I'll post/update the answer here.Befriend
You can also use a Frame, but make sure you enable autoFillBackground, otherwise every child of the widget/panel may get a shadow, and that's not that pretty.Palomo
@zeFree This approach is great, but issue is that drop shadow is not click through. Is there any way to make it click through?Telegonus
From my personal experience developing albert launcher I can recommend to not do this if you have animations in your widget. The shadow effect is going to freak the shit out of your CPU. For this kind of stuff you are probably better off using QML.Rhett
Also the mouse click transparency @EctoRuseff is talking about is something I am still looking for.Rhett
H
0

one not so aweseome solution would be to use the QtCore.Qt.ToolTip window flag instead of the FramelessWindowHint!

at least a little shadow

Hydromedusa answered 5/5, 2015 at 18:57 Comment(1)
It "works" with Qt::Popup, but the window is hidden as soon as it lost focus.Glazunov

© 2022 - 2024 — McMap. All rights reserved.