Search code examples
qtqt-creatorqwidget

Remap context menu call on qwidget


I have my custom widget inherited from QWidget, and I've connected the widget's menu-calling signal to my slot.

connect(m_ontologyView, SIGNAL(customContextMenuRequested(QPoint)), SLOT(showContextMenuSlot(QPoint)));

Now I want user to be able to change button calling the context menu. Normally it's called with right mouse button, but how do I tell the widget to call the menu with a button of my choice?

I'm on Qt 5.4.0


Solution

  • Instead of using QWidget::customContextMenuRequested, you will need to reimplement the widgets mouse event functions, QWidget::mousePressEvent, QWidget::mouseReleaseEvent and QWidget::mouseMoveEvent. Inside of these events, you can then show you menu using QMenu::popup. (The point can be extracted from the mouse events).