NameError: global name 'QContextMenuEvent' is not defined
<p>I want to add a right click menu.</p>

<pre><code>def contextMenuEvent(self,event):
    global posX
    global posY
    global selections,scan_widget

    if event.reason() == QContextMenuEvent.Mouse:
        menu = QMenu(self)
        clear = menu.addAction('Clear')

        for i in selections:
            self.buttonLabels.append(menu.addAction(i))

        deleteBox = menu.addAction('Delete Box')
        changeId = menu.addAction('Change Id')
        cancel = menu.addAction('Cancel')
        action = menu.exec_(self.mapToGlobal(event.pos()))
        for i,key in enumerate(self.buttonLabels):
            if action == key:
                self.annotClass = selections[i]
                self.annotEnabled = True
        if action == deleteBox:
            self.deleteEnabled = True
        elif action == changeId:
            #Call the textbox
            self.newBoxId = textBox()
            self.newBoxId.setGeometry(QRect(500, 100, 300, 100))
            self.newBoxId.show()
        elif action == cancel:
            pass
        elif action == clear:
            self.annotClass = 'Clear'
            self.annotEnabled = True

        self.posX_annot = event.pos().x()
        self.posY_annot = event.pos().y()

        posX = event.pos().x()
        posY = event.pos().y()

        self.repaint()
        self.buttonLabels = []
    self.annotEnabled = False
</code></pre>

<p>But give me back this error: </p>

<blockquote>
  <p>NameError: global name 'QContextMenuEvent' is not defined</p>
</blockquote>

<p>Then, I added QContextMenuEvent as import:</p>

<pre><code>from PyQt5.QtWidgets import (QApplication, QComboBox, QHBoxLayout, QPushButton,QSizePolicy, QVBoxLayout, QWidget,QLineEdit, QInputDialog, QMenu,QContextMenuEvent)
</code></pre>

<p>But,</p>

<blockquote>
  <p>ImportError: cannot import name QContextMenuEvent</p>
</blockquote>

<p>What am I doing wrong?</p>

<p>I replaced </p>

<p><code>if event.reason() == QContextMenuEvent.Mouse:</code>  </p>

<p>with</p>

<p><code>if event.button == Qt.RightButton:</code></p>

python menu pyqt5