QLIstWidget 右键菜单, 可以通过 下方槽函数实现
customContextMenuRequested(QPoint);
具体实现代码:
在初始化函数中加入:
ui->list1->setContextMenuPolicy(Qt::CustomContextMenu);
然后鼠标在ListWidget控件上右键, 转到槽
void Files_Manager::on_list1_customContextMenuRequested(const Point &pos)
{
QListWidgetItem *item = ui->list1->itemAt(pos);
if(item == nullptr)
return;
QMenu *menu = new QMenu(this);
QAction *act = new QAction("菜单项目", this);
menu->addAction(act);
connect(act, SIGNAL(triggered()), this, SLOT(my_slot()));
menu->exec(Cursor::pos()); //在鼠标位置弹出菜单
delete menu;
delete act;
}