QWidget及其子类都可有右键菜单
1.设置标志
在widget初始化的时候
setContextMenuPolicy(Qt::CustomContextMenu);
设置为自定义菜单模式
2.在需要的地方进行信号关联
connect( mBrowserView, &CivDataSourceTreeView::customContextMenuRequested, this, &CivDataSourceDockWidget::showContextMenu );
3,实现槽函数
QMenu *menu = new QMenu( this ); QAction* proDelete = new QAction("删除", this); menu->addAction(proDelete); connect(proDelete, &QAction::triggered, this, &CivDataSourceDockWidget::deletePro); menu->popup( mBrowserView->mapToGlobal( pt ) );