PySide6.QtGui.QContextMenuEvent

class QContextMenuEvent

The QContextMenuEvent class contains parameters that describe a context menu event.

Details

A context menu event is sent when a user performs an action that should open a contextual menu:

  • clicking the right mouse button

  • pressing a dedicated keyboard menu key (if the keyboard has one, such as the menu key on standard 104-key PC keyboards)

  • pressing some other keyboard shortcut (such as “Ctrl+Return” by default on macOS 15 and newer)

The expected context menu should contain actions that are relevant to some content within the application (the “context”). In Qt, the context is at least the particular widget or Qt Quick Item that receives the QContextMenuEvent . If there is a selection, that should probably be treated as the context. The context can be further refined using pos() to pinpoint the content within the widget, item or selection.

Widgets can override QWidget::contextMenuEvent() to handle this event. Many widgets already do that, and have useful context menus by default. Some widgets have a function such as createStandardContextMenu() to populate the default set of actions into a QMenu, which can be customized further in your subclass and then shown.

In Qt Quick, the event can be handled via the ContextMenu attached property. Some QtQuick.Controls Controls already provide context menus by default.

Unlike most synthetic events (such as a QMouseEvent that is sent only after a QTouchEvent or QTabletEvent was not accepted), QContextMenuEvent is sent regardless of whether the original mouse or key event was already handled and accepted. This is to accommodate the Windows UI pattern of selecting some kind of items (icons, drawing elements, or cells in an Item View) using the right mouse button (clicking or dragging), and then getting a context menu as soon as you release the right mouse button. (The actions on the menu are meant to apply to the selection.) Therefore, on Windows the QContextMenuEvent is sent on mouse release; while on other platforms, it’s sent on press. Qt follows the platform convention by default.

There are also some Qt Quick Controls such as Pane that accept mouse events, and nevertheless receive a QContextMenuEvent after a mouse press or click.

If you prefer to support the press-drag-release UI pattern to open a context menu on press, and drag over a menu item to select it on release, you will need to do that by handling QMouseEvents directly (by overriding virtual functions in QWidget subclasses, or using TapHandler to open a Menu in Qt Quick); and then the QContextMenuEvent will be redundant when the reason() is Mouse. You should ignore() the event in that case; but you should still ensure that the widget, custom control or application can respond to a QContextMenuEvent that comes from the platform-specific keyboard shortcut.

When a QContextMenuEvent is ignored, Qt attempts to deliver it to other widgets and/or Items under the position (which is usually translated from the cursor position).

Inheritance diagram of PySide6.QtGui.QContextMenuEvent

Synopsis

Methods

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

class Reason

This enum describes the reason why the event was sent.

Constant

Description

QContextMenuEvent.Reason.Mouse

The mouse caused the event to be sent. Normally this means the right mouse button was clicked, but this is platform dependent.

QContextMenuEvent.Reason.Keyboard

The keyboard caused this event to be sent. On Windows, this means the menu button was pressed.

QContextMenuEvent.Reason.Other

The event was sent by some other means (i.e. not by the mouse or keyboard).

__init__(arg__1)
Parameters:

arg__1QContextMenuEvent

__init__(reason, pos)
Parameters:

Note

This function is deprecated.

Use the other constructor instead (global position is required).

Constructs a context menu event object with the accept parameter flag set to false.

The reason parameter must be Mouse or Keyboard .

The pos parameter specifies the mouse position relative to the receiving widget.

The globalPos() is initialized to pos() , which may not be appropriate. Use the other constructor to specify the global position explicitly.

__init__(reason, pos, globalPos[, modifiers=Qt.NoModifier])
Parameters:

Constructs a context menu event object with the accept parameter flag set to false.

The reason parameter must be Mouse or Keyboard .

The pos parameter specifies the mouse position relative to the receiving widget. globalPos is the mouse position in absolute coordinates. The modifiers holds the keyboard modifiers.

globalPos()
Return type:

QPoint

Returns the global position of the mouse pointer at the time of the event.

See also

x() y() pos()

globalX()
Return type:

int

Returns the global x position of the mouse pointer at the time of the event.

globalY()
Return type:

int

Returns the global y position of the mouse pointer at the time of the event.

pos()
Return type:

QPoint

Returns the position of the mouse pointer relative to the widget that received the event.

Note

If the QContextMenuEvent did not come from the right mouse button, pos() may be null.

See also

x() y() globalPos()

reason()
Return type:

Reason

Returns the reason for this context event.

x()
Return type:

int

Returns the x position of the mouse pointer, relative to the widget that received the event.

See also

y() pos()

y()
Return type:

int

Returns the y position of the mouse pointer, relative to the widget that received the event.

See also

x() pos()