实现qt中qtableview 已选择行区域右键点击弹出menu

1.先设置tableview 样式(ui.tv_searchList 为tableview控件)

//设置systemTableView的菜单格式 
//比较重要 只有这样设置 才能使用信号SIGNAL(customContextMenuRequested(QPoint))
ui.tv_searchList->setContextMenuPolicy(Qt::CustomContextMenu); 
//设置tableview一次只能可选一行
ui.tv_searchList>setSelectionBehavior(QAbstractItemView::SelectRows);
ui.tv_searchList>setSelectionMode(QAbstractItemView::SingleSelection);
2.实例menu (注意menu设为tableview的子控件)
action_fillUserInfo = new QAction ( this);
action_fillUserInfo ->setText ( QStringLiteral( "设置密码" ));
popMenu = new QMenu(ui.tv_searchList);
popMenu->addAction(action_fillUserInfo);
3.信号槽编写
新建信号连接
connect(ui.tv_searchList, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotContextMenu(QPoint)));            //添加右键菜单

信号直接用

曹函数需要自己实现
void DeviceImportDialog::slotContextMenu(QPoint pos){
QModelIndex index = ui.tv_searchList->indexAt(pos);

if (index.isValid())
{
popMenu->exec(QCursor::pos()); // 菜单出现的位置为当前鼠标的位置
}
}
附:
action_fillUserInfo  为QAction
popMenu 为QMenu

效果:
能在tableview有可选择行的情况下弹出menu菜单
  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值