QObject::connect(ui.tableView, &QTableView::customContextMenuRequested, this, &DemographicStatistics::slotContexMenu);
QAction* m_action = new QAction(QString::fromLocal8Bit("aa"));;
QMenu* m_contextMenu = new QMenu(this);
m_contextMenu->addAction(m_action);
void slotContexMenu(const QPoint& pos)
{
m_currentIndex = ui.tableView->indexAt(pos);
if (m_currentIndex.isValid())
{
m_contextMenu->exec(QCursor::pos());
}
}
QTableview右键菜单(qt5)
于 2022-10-24 22:13:47 首次发布
本文展示了如何在QT环境中使用QTableView组件响应右键点击事件,创建并显示自定义上下文菜单。通过`QObject::connect`设置信号与槽,实现在表格视图中触发`customContextMenuRequested`事件时调用`slotContexMenu`函数。在该函数中,获取当前选中索引并执行菜单。这是一个基础的QT界面交互功能的实现。
摘要由CSDN通过智能技术生成