mainwindow.cpp
void MainWindow::on_Library_clicked()
{
QMenu menu(this);
QMenu *LanguageMenu = menu.addMenu(tr("&Language"));
QAction *EnMenu = new QAction(tr("&English"), this);
QAction *ZhCnMenu = new QAction(tr("Chinese &Simple"),this);
LanguageMenu->addAction(EnMenu);
LanguageMenu->addAction(ZhCnMenu);
QMenu *Skin = menu.addMenu(tr("&Skin"));
QAction *WBAction = new QAction(tr("&White and Blue"),this);
QAction *OtherAction = new QAction(tr("&Other"),this);
Skin->addAction(WBAction);
Skin->addAction(OtherAction);
QPainterPath path;
QRectF rect = QRectF(0,0,menu.sizeHint().width(),menu.sizeHint().height());
path.addRoundedRect(rect,6,6);
QPolygon polygon= path.toFillPolygon().toPolygon();//获得这个路径上的所有的点
QRegion region(polygon);//根据这个点构造这个区域
menu.setMask(region);
QPoint pos;
pos.setX(0);
pos.setY(-menu.sizeHint().height());
menu.exec(ui->Library->mapToGlobal(pos));
}
Qss文件
QMenu {border-radius: 5px;
border: 1px solid rgb(110, 110, 110);}
这是我在做QMenu圆角处理过程中写的代码,使用遮罩做的,没有用到重构,有些瑕疵的是左下和左上角有些白边,这里如果没有使用Qss做圆角边可以避免瑕疵,但是就没有边了,而且圆角要超过6,否则也会出现瑕疵,期待高手解决。下面附上效果图
