QT 右键点击表格中某行在对应位置弹出相应菜单

首先声明这两个变量   
    QMenu* m_menu;
    QAction *action_setTime;

在要使用的位置中加入

    action_setTime = new QAction ( this);
    action_setTime->setText(QStringLiteral("Reset last job time"));
    m_menu = new QMenu(ui->tableView);
    m_menu->addAction(action_setTime);
    connect(ui->tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotContextMenu(QPoint)));//添加右键菜单
    connect(m_menu,SIGNAL(triggered(QAction*)),this,SLOT(trigerMenu(QAction*)));//响应动作

槽函数 要记得在类里声明槽函数

void XX::slotContextMenu(QPoint pos)
{
    QModelIndex index = ui->tableView->indexAt(pos);

    if (index.isValid())
    {
    m_menu->popup(QCursor::pos()); // 菜单出现的位置为当前鼠标的位置
    }
}
void XX::trigerMenu(QAction * pos)//响应点击菜单动作
{
  
   action_setTime->setCheckable(true);
   action_setTime->setChecked(true);
   QModelIndex index = ui->tableView->currentIndex();//获取当前点击位置
   int i=index.row();
   .....   //添加自己的响应的需求
}

 

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值