It's fine if you want to use only one menu/action instance for your different controls. You just need a way to know which control is triggering the menu so that you can reference it when the action is selected. One way to do that is to track the current context widget using "self.sender()" when your context menu is triggered:
class MyTool(QtGui.QWidget): def __init__(self, parent=None): super(MyTool, self).__init__(parent = parent) ... self._contextWidget = None self.ui.xLineEdit.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.ui.xLineEdit.customContextMenuRequested.connect(self.custom_menu) ... def set_key(self): lineEdit = self._contextWidget if not lineEdit: return ... def custom_menu(self, point): self._contextWidget = self.sender() self.popMenu.exec_(QtGui.QCursor.pos()) Every time you trigger the menu, you save the widget that triggered it. Then you can reference it if an action is selected from the menu. Justin On Fri, Jun 21, 2019 at 11:45 AM likage <dissidia....@gmail.com> wrote: > Hi everyone, I created a tool using Qt Designer, where it has 3 QLineEdits > that is catered for translateX, translateY and translateZ. > For each QLineEdit, I have created a context menu that allows me to set a > keyframe for one of the above attribute depending on User's choice. > > So instead of writing 3 separate functions that catered to each attribute, > I thought of 'recycling' them by using 1 method, but I am having issues > with it as I am not very sure if it will be possible since I am using a > single QAction. > > class MyTool(QtGui.QWidget): > def __init__(self, parent=None): > super(MyTool, self).__init__(parent = parent) > # Read off from convert uic file. > self.ui = Ui_MyWidget() > self.ui.setupUi(self) > > # translateX > self.ui.xLineEdit.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) > > self.ui.xLineEdit.customContextMenuRequested.connect(self.custom_menu) > # translateY > self.ui.yLineEdit.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) > > self.ui.yLineEdit.customContextMenuRequested.connect(self.custom_menu) > # translateZ > self.ui.zLineEdit.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) > > self.ui.zLineEdit.customContextMenuRequested.connect(self.custom_menu) > > self.popMenu = QtGui.QMenu(self) > set_key_action = QtGui.QAction("Set Key at Current Frame", self) > > # I am having issues here.. > set_key_action.triggered.connect(self.set_key) > > self.popMenu.addAction(set_key_action) > > ... > ... > > def set_key(self, attr): > # assuming I am trying to effect this locator1 that already exists > in the scene > current_item = "|locator1" > cmds.setKeyframe("{0}.{1}".format(current_item, attr)) > > def custom_menu(self, point): > self.popMenu.exec_(QtGui.QCursor.pos()) > > I thought of using `functools` so that my code will be something like : > class MyTool(QtGui.QWidget(: > def __init__(self, parent=None): > ... > ... > functools.partial(self.set_key, "tx") > > def set_key(self, attr): > if attr == "tx": > # setkeyframe to translateX > elif attr == "ty": > # setkeyframe to translateY > > Again, because it is only a single QAction and hence I was stumped... Or > will it be better for me to stick in using 3 separate functions instead? > > -- > You received this message because you are subscribed to the Google Groups > "Python Programming for Autodesk Maya" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to python_inside_maya+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/python_inside_maya/a3c45192-ec1e-4a85-87de-63ac2de131a9%40googlegroups.com > <https://groups.google.com/d/msgid/python_inside_maya/a3c45192-ec1e-4a85-87de-63ac2de131a9%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group. To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1vUP79O7jBJhDPEWF13%3DeCKV%3DBESQDc8g7Gz%2BYvaKC3Q%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.