标签:
1.在.h文件中包含相关头文件
#include <QMenu>
#include <QContextMenuEvent>
2.在.h文件中定义动作对象
QAction *edit
QAction *del;
3.在.h文件中声明私有方法
void contextMenuEvent(QContextMenuEvent *e);
4.在.h文件中增加右键点击后动作槽声明
private slots: void edit_line(); void del_line();
5.在cpp文件的构建函数中实例化动作对象
edit = new QAction(QIcon(":/icon/edit.png"), tr("编辑"), this); connect(edit, SIGNAL(triggered()), this, SLOT(edit_line())); del = new QAction(QIcon(":/icon/delete.png"), tr("删除"), this); connect(del, SIGNAL(triggered()), this, SLOT(del_line()));
6.在cpp文件中实现右键的事件
void MainWindow::contextMenuEvent(QContextMenuEvent *e) { QMenu *menu = new QMenu();
menu->setFixedWidth(120); menu->addAction(edit); menu->addAction(del); menu->exec(e->globalPos()); delete menu; }
最后在cpp文件中实现点击后的处理
void MainWindow::edit_line() { //put your code } void MainWindow::del_line() { //put your code }
标签:
原文地址:http://www.cnblogs.com/Jim-william/p/5077201.html