PDA

View Full Version : Multiple Context Menus



mclark
24th December 2008, 18:10
Season's Greetings (I guess this works any time of the year;)).

I'm using Qt 4.4.2 on a Windows platform and need to do an unconventional drag & drop.

Left AND right mouse button drags between two QTableWidgets are allowed. When releasing the buttons in the target table, the LMB does the add/overwrite but the RMB must display a context menu to allow the user to pick specific actions.

My normal handling of the mouseReleaseEvent shows one context menu but the mouse release of a RMB drop should show a different menu. I believe I can handle the menu item switching with setVisible() calls.

The issue here is that in my dropEvent() handler I can determine which button was used for the drag...
if ( event->mouseButtons() & Qt::RightButton )
// display context menu...but I don't know how to display a context menu at this point when the RMB is released. Can someone point me in the right direction to display a context menu from within the drop event handler?

spirit
24th December 2008, 18:33
you need to do next things:
1. set context menu policy for widget for which context menu will be displayed


...
m_myWidget->setContextMenuPolicy(Qt::CustomContextMenu);
...

2. connect signal customContextMenuRequested with slot in which context menu will be created


...
connect(m_myWidget, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(updateContextMenu(const QPoint &)));
...

3. create needed context menu and show it


void AnotherMyWidget::updateContextMenu(const QPoint &pos)
{
QMenu menu(tr("Context Menu"), this);
//add actions to menu and create connections
menu.exec(m_myWidget->mapToGlobal(pos));
}

mclark
24th December 2008, 20:51
you need to do next things:
1. set context menu policy for widget for which context menu will be displayed
2. connect signal customContextMenuRequested with slot in which context menu will be created
3. create needed context menu and show it


This makes the context menu switching easier but doesn't solve the basic problem of making a context menu show up when releasing the right mouse button at the end of a drag.

This is the order of operations I'm looking for:

right-click and drag from TableA to TableB
release the right mouse button (now in the dropEvent() code)
a context menu is displayed
user makes a selection from the context menu
drop is processed based upon that selection

spirit
24th December 2008, 20:55
ok, just call


void AnotherMyWidget::updateContextMenu(const QPoint &pos)

after mouse releadse. (you have current point just pass it in method)