Home > Enterprise >  QT5 Cannot get different custom context menus to work for different tables
QT5 Cannot get different custom context menus to work for different tables

Time:09-17

I am trying to get multiple (3) custom context menus to work, each for a different table view.

My code works fine in debug but in release I am not getting the different context menus - the best I have managed to get is either the first menu working (and the others disabled) or displaced menus (i.e. the menu is offset relative to the top left corner of the screen not at the cursor).

The code:

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

I have tried looking / searching for the same problem, but failed to resolve the issue.

CodePudding user response:

I have found a way to make it work although I still do not understand why my code failed.

The way to make it work was to swap out the following:

menuNameOut->popup(pTableW_EmuNameOut->viewport()->mapToGlobal(pos));
menuNameIn->popup(pTableW_EmuNameIn->viewport()->mapToGlobal(pos));
menuParam->popup(pTableW_Param->viewport()->mapToGlobal(pos));

for:

menuNameOut->exec(QCursor::pos());
menuNameIn->exec(QCursor::pos());
menuParam->exec(QCursor::pos());
  • Related