一、首先将菜单设置为用户菜单

Qt QlistWidget如何实现用户右键菜单_右键


二、右键转为槽函数​​customContextMenuRequested​

Qt QlistWidget如何实现用户右键菜单_右键_02


三、代码实现槽函数

void MainWindow::on_listWidget_customContextMenuRequested(const QPoint &pos)
{
QMenu *menu = new QMenu(this);
menu->addAction(ui->actionAdd);
menu->addAction(ui->actionClear);
menu->addAction(ui->actionDelete);
menu->addAction(ui->actionInsert);
menu->addAction(ui->actionInit);
menu->addSeparator();
menu->addAction(ui->actionSelAll);
menu->addAction(ui->actionSelInv);
menu->addAction(ui->actionSelNone);
menu->addAction(ui->actionSelPopMenu);
menu->exec(QCursor::pos());
delete menu;
}