在*.h添加头文件
#include <QContextMenuEvent>
定义私有函数
void contextMenuEvent(QContextMenuEvent *e);
定义Action
QAction *showpoint;
QAction *showline;
QAction *showtriangle;
定义槽函数
void DrawPoint();
void DrawLine();
void DrawTriangle();
在*.cpp文件中创建Action
showpoint = new QAction(codec->toUnicode("点"), this);
showpoint->setIcon(QIcon(":/GLWIDGET/Resources/point.png"));
showpoint->setStatusTip(codec->toUnicode("以点显示"));
connect(showpoint, SIGNAL(triggered()), this, SLOT(DrawPoint()));
showline = new QAction(codec->toUnicode("线"), this);
showline->setIcon(QIcon(":/GLWIDGET/Resources/line.png"));
showline->setStatusTip(codec->toUnicode("以线显示"));
connect(showline, SIGNAL(triggered()), this, SLOT(DrawLine()));
showtriangle = new QAction(codec->toUnicode("面"), this);
showtriangle->setIcon(QIcon(":/GLWIDGET/Resources/triangle.png"));
showtriangle->setStatusTip(codec->toUnicode("以面显示"));
connect(showtriangle, SIGNAL(triggered()), this, SLOT(DrawTriangle()));
void GLWIDGET::contextMenuEvent(QContextMenuEvent *e)
{
QMenu *menu = new QMenu();
menu->setFixedWidth(100);
menu->addAction(showpoint);
menu->addAction(showline);
menu->addAction(showtriangle);
menu->exec(e->globalPos());
delete menu;
}