def contextMenuEvent(self, event):
        """
        Add menu action:
        * 'Show line numbers'
        * 'Save to file'
        """
        menu = QtWidgets.QTextEdit.createStandardContextMenu(self)

        # create max.lines spin box:
        w = QtWidgets.QWidget()
        l = QtWidgets.QHBoxLayout()
        w.setLayout(l)
        e = QtWidgets.QSpinBox()
        e.setRange(1, 1e6)
        e.setValue(self.MAXLINES)
        e.valueChanged.connect(self.document().setMaximumBlockCount)
        l.addWidget(QtWidgets.QLabel('Max. lines'))
        l.addWidget(e)

        # add spinbox to menu:
        a = QtWidgets.QWidgetAction(self)
        a.setDefaultWidget(w)
        menu.addAction(a)

        menu.exec_(event.globalPos())
Add menu action:
        * 'Show line numbers'
        * 'Save to file'