Nix
Nix

Reputation: 515

PySide\PyQt - Menu Add Action at index (before or between)

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.

Upvotes: 3

Views: 3040

Answers (1)

Nix
Nix

Reputation: 515

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

PySide - Insert action

Upvotes: 3

Related Questions