效果如下:
可以看到增加了圆角、边框、阴影、背景色,,更多的样式可以在qss中继续设置
主要思路:
1、QMenu弹出来其实就是一个窗口
所以首先设置其窗口flag为Qt::FramelessWindowHint和Qt::NoDropShadowWindowHint,去掉自带的样式和阴影
然后设置其attribute为背景透明Qt::WA_TranslucentBackground
2、然后在样式表中设置圆角和边框还有背景色
最重要的是还要设置margin,这样后面的阴影才能显示出来
3、添加阴影就行了,利用QGraphicsDropShadowEffect
代码如下:
QMenu m;
m.setWindowFlags(m.windowFlags()|Qt::FramelessWindowHint|Qt::NoDropShadowWindowHint);
m.setAttribute(Qt::WA_TranslucentBackground);
m.setStyleSheet(R"(
QMenu{
margin:10px;
border:1px solid #94a6ca;
background-color:#e9eeff;
border-radius:3px;
}
)");
QGraphicsDropShadowEffect* s=new QGraphicsDropShadowEffect(&m);
s->setColor(Qt::red);
s->setOffset(1,1);
s->setBlurRadius(10);
m.setGraphicsEffect(s);
m.addAction("111");
m.addAction("222");
m.exec(QPoint(500,500));