c++ - QT5 无法让不同的自定义上下文菜单适用于不同的表
问题描述
我正在尝试让多个 (3) 自定义上下文菜单工作,每个菜单用于不同的表格视图。
我的代码在调试中工作正常,但在发布时我没有得到不同的上下文菜单——我设法得到的最好的要么是第一个菜单工作(而其他人被禁用),要么是移位的菜单(即菜单相对于顶部偏移屏幕左角不在光标处)。
编码:
void DisplayWidget::Init()
{
// ParameterData Table
pLabel_Param = new QLabel(tr("PARAMETER DATABASE"));
pTableW_Param = new QTableWidget(this);
// EmuNameIn Table
pLabel_EmuNameIn = new QLabel(tr("EMULATOR NAME IN"));
pTableW_EmuNameIn = new QTableWidget(this);
// EmuNameOut Table
pLabel_EmuNameOut = new QLabel(tr("EMULATOR NAME OUT"));
pTableW_EmuNameOut = new QTableWidget(this);
// Setup context menus
pTableW_Param->setContextMenuPolicy(Qt::CustomContextMenu);
connect(pTableW_Param, SIGNAL(customContextMenuRequested(QPoint)), SLOT(customMenuRequested_Param(QPoint)));
pTableW_EmuNameIn->setContextMenuPolicy(Qt::CustomContextMenu);
connect(pTableW_EmuNameIn, SIGNAL(customContextMenuRequested(QPoint)), SLOT(customMenuRequested_EmuNameIn(QPoint)));
pTableW_EmuNameOut->setContextMenuPolicy(Qt::CustomContextMenu);
connect(pTableW_EmuNameOut, SIGNAL(customContextMenuRequested(QPoint)), SLOT(customMenuRequested_EmuNameOut(QPoint)));
}
void DisplayWidget::customMenuRequested_Param(QPoint pos)
{
if(!pTableW_Param || (sizeOfTable_Param == 0)) return;
QModelIndex index = pTableW_Param->indexAt(pos);
QMenu *menuParam = new QMenu(this);
if(IsEmuValid()) menuParam->addAction(pAct_AddParam);
menuParam->addAction(pAct_SearchTable_Param);
menuParam->popup(pTableW_Param->viewport()->mapToGlobal(pos));
}
void DisplayWidget::customMenuRequested_EmuNameIn(QPoint pos)
{
if(!pTableW_EmuNameIn || !IsEmuValid() || (sizeOfTable_EmuNameIn == 0)) return;
QModelIndex index = pTableW_EmuNameIn->indexAt(pos);
QMenu *menuNameIn = new QMenu(this);
menuNameIn->addAction(pAct_DeleteFromNameIn);
menuNameIn->addAction(pAct_Toggle_InToOut);
menuNameIn->addAction(pAct_SearchTable_EmuNameIn);
menuNameIn->addAction(pAct_SortTable_EmuNameIn);
menuNameIn->popup(pTableW_EmuNameIn->viewport()->mapToGlobal(pos));
}
void DisplayWidget::customMenuRequested_EmuNameOut(QPoint pos)
{
if(!pTableW_EmuNameOut || !IsEmuValid() || (sizeOfTable_EmuNameOut == 0)) return;
QModelIndex index = pTableW_EmuNameOut->indexAt(pos);
QMenu *menuNameOut = new QMenu(this);
menuNameOut->addAction(pAct_DeleteFromNameOut);
menuNameOut->addAction(pAct_Toggle_OutToIn);
menuNameOut->addAction(pAct_SearchTable_EmuNameOut);
menuNameOut->addAction(pAct_SortTable_EmuNameOut);
menuNameOut->popup(pTableW_EmuNameOut->viewport()->mapToGlobal(pos));
}
我曾尝试寻找/搜索相同的问题,但未能解决问题。
解决方案
尽管我仍然不明白为什么我的代码失败了,但我找到了一种让它工作的方法。
使其工作的方法是更换以下内容:
menuNameOut->popup(pTableW_EmuNameOut->viewport()->mapToGlobal(pos));
menuNameIn->popup(pTableW_EmuNameIn->viewport()->mapToGlobal(pos));
menuParam->popup(pTableW_Param->viewport()->mapToGlobal(pos));
为了:
menuNameOut->exec(QCursor::pos());
menuNameIn->exec(QCursor::pos());
menuParam->exec(QCursor::pos());
推荐阅读
- python - matplotlib 图中垂直轴上的自变量(垂直 x 轴)
- javascript - onClick 在 React 中没有被触发
- javascript - 如何通过在 PHP 或 Javascript 中使用 API 来获取 Instagram 用户名可用性?
- sql - Snowflake中变量的限制是什么
- office365 - 云端硬盘搜索不适用于客户端凭据令牌
- c - 为什么时间会变?
- sql - 无法创建索引,在物化视图上锁定
- tensorflow - 在 Keras 中连接两个嵌入层时,如何解决“indices[1,0] = 66 is not in [0, 25)”错误?
- javascript - JavaScript 计时器的问题
- eslint - 如何在 VS 代码中关闭 ESlinting 换行符