Affex
Affex

Reputation: 21

Qt5 ContextMenuPolicy for Python

I'm looking for the right way to set up a ContextMenuPolicy. for a Qt5 "QtWidgets.QTableWidget()"

Currently, I've set up the code like this.

self.file_table = QtWidgets.QTableWidget()
self.file_table.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)

self.context_import = self.file_table.addAction("Import")
self.context_import.triggered.connect(self.launchHelp)

I do have a QMenu working in my script using this exact method, but as soon as I apply this method on a QTableWidget the script stops working, without even issuing an error.

It's worth noting that this script is running inside "SideFX Houdini" a visual effects software, and uses PySide2.

Anyone know a good way to setup a ContextMenu using Qt5. There seems to be limited examples out there. And I'm still learning how to read the Qt5 documentation: http://doc.qt.io/qt-5/qt.html#ContextMenuPolicy-enum

Any help is greatly appreciated!

A

Upvotes: 0

Views: 1127

Answers (1)

Affex
Affex

Reputation: 21

Turns out I needed to specify a QAction object.

I managed to fix this issue using the following code:

self.files_table.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
self.context_import = QtWidgets.QAction("Import", self)
self.context_import.triggered.connect(self._import_action)
self.files_table.addAction(self.context_import)

Upvotes: 2

Related Questions