Hi,
There are a couple different modes on a widget for controlling context menus.
The default policy, which you are using in your example, uses the contextMenuEvent on the widget to create the default menu.
The actions mode is the one that will look at the actions() on the widget and create a menu from that. So you are adding an action that isn't being considered. The problem is, if you were to switch to the actions mode then you wouldn't get any of the default actions if they aren't stored in that widget actions list. The goal you have is to augment the default actions menu. In a general case this would be more work because you would need to subclass the widget, reimplement the contextMenuEvent, and defer a callback after the menu has been show so that you can look it up and modify it. Fortunately for QLineEdit we don't have to take that approach, as it provides a method to create the default context menu instance:
createStandardContextMenu
Something like this:
class MyLineEdit(QtWidgets.QLineEdit):
def contextMenuEvent(self, event):
menu = self.createStandardContextMenu()
menu.addAction(...)
menu.exec_(event.globalPos())
Some answers online suggest you could just reimplement createStandardContextMenu to return a modified menu. But createStandardContextMenu is not a virtual method as per the api docs, so it would only work reliably part of the time, when it is being triggered from python as opposed to internally via Qt C++
Justin