3

I'm creating a custom context menu for a widget in PySide, and I want to preserve Standard menu options, but I want to put them after custom actions.

Is there a way to add actions to QMenu and set specific order for them?

Here's my current code:

def buildRightClickMenu(self):

    self.textBox.setContextMenuPolicy(Qt.CustomContextMenu)
    self.textBox.customContextMenuRequested.connect(self.contextMenuRequested)

    self.actionSave = QAction(self)
    self.actionSave.setText("Save File")
    self.actionSave.triggered.connect(self.saveFile)

    self.actionOpen = QAction(self)
    self.actionOpen.setText("Open File")
    self.actionOpen.triggered.connect(self.openFile)


    self.menu = self.textBox.createStandardContextMenu()

    self.menu.addSeparator()
    self.menu.addAction(self.actionOpen)

As expected, this first creates default menu options, and adds a separator and actionOpen afterwards. I want to put actionOpen at the start of the context menu, followed by default actions for the widget.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Nix
  • 465
  • 1
  • 7
  • 16

1 Answers1

3

It is PySide.QtGui.QWidget.insertAction(before, action), in QWidget class.

PySide - Insert action

Nix
  • 465
  • 1
  • 7
  • 16