简体   繁体   English

适用于Python的Qt5 ContextMenuPolicy

[英]Qt5 ContextMenuPolicy for Python

I'm looking for the right way to set up a ContextMenuPolicy. 我正在寻找设置ContextMenuPolicy的正确方法。 for a Qt5 "QtWidgets.QTableWidget()" 为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. 我确实使用此确切方法在脚本中运行了QMenu,但是一旦在QTableWidget上应用此方法,脚本便会停止工作,甚至不会发出错误。

It's worth noting that this script is running inside "SideFX Houdini" a visual effects software, and uses PySide2. 值得注意的是,该脚本正在视觉效果软件“ SideFX Houdini”中运行,并使用PySide2。

Anyone know a good way to setup a ContextMenu using Qt5. 任何人都知道使用Qt5设置ContextMenu的好方法。 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 而且我仍在学习如何阅读Qt5文档: http ://doc.qt.io/qt-5/qt.html#ContextMenuPolicy-enum

Any help is greatly appreciated! 任何帮助是极大的赞赏!

A 一种

Turns out I needed to specify a QAction object. 原来我需要指定一个QAction对象。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM