qt Qtableview 右键弹出菜单

界面showDlg中有 Qtableview (m_ui.tableView)
要求右键弹出菜单隐藏某一行数据代表的图片

//构造函数 设置右键菜单,并绑定槽函数
showDlg::showDlg()
{
	m_ui.tableView->setContextMenuPolicy(Qt::CustomContextMenu); //可弹出右键菜单
	connect(m_ui.tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotContextMenu(QPoint)));
}

//右键菜单的槽函数
void showDlg::slotContextMenu(QPoint pos)
{
	// 获取右键时 Qtableview 中的项
	QModelIndex index = m_ui.tableView->indexAt(pos);
	
	// new一个菜单
	QMenu* popMenu = new QMenu(m_ui.tableView);
	if (index.isValid())
	{
		// 新建一个 QAction(可建多个),并设置显示的文本
		QAction* actionUpdateInfo = new QAction();
		actionUpdateInfo->setText(QString(QStringLiteral("隐藏缺陷")));
		
		// 把获取到的行数存储到 QAction中
		actionUpdateInfo->setData(index.row());
		popMenu->addAction(actionUpdateInfo);
		
		// QAction 绑定槽函数,当点击QAction时触发
		connect(actionUpdateInfo, SIGNAL(triggered()), this, SLOT(SlotAction_triggered()));
		
		// 菜单显示到鼠标的位置
		popMenu->exec(QCursor::pos());
	}
	//释放内存
	QList<QAction*> list = popMenu->actions();
	foreach(QAction * pAction, list) delete pAction;
	delete popMenu;
}
//点击QAction时触发
void showDlg::SlotAction_triggered()
{
	QAction* pEven = qobject_cast<QAction*>(this->sender()); //this->sender()就是发信号者 QAction
	
	//获取到的行数
	int currentRow = pEven->data().toInt();
	
	//显示
	m_ui.label->setSelect(currentRow, true);
}
  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值