1.头文件
#include<QMenu>
#include<QAction>
QMenu *_ExLogMenu; //异常日志右键清除菜单
QAction *_ExLogClearMenu;
2.CPP文件
//设置listview的属性为自定义右键文本菜单
ui->listView_RunLogMsg->setContextMenuPolicy(Qt::CustomContextMenu);
_RunLogMenu = new QMenu(this);
_RunLogClearMenu = new QAction(this);
_RunLogClearMenu->setText(QString::fromLocal8Bit("清除运行日志"));
_RunLogMenu->addAction(_RunLogClearMenu);
//链接界面的鼠标右键显示菜单
connect(ui->listView_RunLogMsg,&QListView::customContextMenuRequested,[=](){
_RunLogMenu->exec(QCursor::pos());
});
//连接要点击执行的操作
connect(_RunLogClearMenu,&QAction::triggered,[=](){
MainWindow::m_RunLogModel->removeRows(0,MainWindow::m_RunLogModel->rowCount());
});