界面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);
}