问题
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...
回答1:
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:
回答2:
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.
回答3:
one not so aweseome solution would be to use the QtCore.Qt.ToolTip
window flag instead of the FramelessWindowHint
!
来源:https://stackoverflow.com/questions/12347776/how-to-put-a-shadow-to-a-frameless-window-in-qt