Qt添加右键菜单

在*.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;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章