Hey guys,
I hope you are well !
I'm currently trying to create quite a lot of contextMenu for lots of
different buttons in a maya UI I'm working on.
The issue is that I've a long list of buttons and I need the same
contextMenu for all of them.... What I'm doing now is not really elegant,
that's why I'm asking if you could help me...
Because with the following process, my script will be reallllyyy realllyyy
long but I can't figure out how to clean that properly :(
Here's an example of my code :
from PyQt4 import QtCore,QtGui,uic
from functools import partial
import os
class MyWidget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
uiFilePath = os.path.join(os.path.dirname(__file__),"myfile.ui")
self.ui = uic.loadUi(uiFilePath, self)
'''
Right Click Menu
'''
self.ui.buttonA.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.ui.buttonA.customContextMenuRequested.connect(self.buttonAMenu)
self.ui.buttonB.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.ui.buttonB.customContextMenuRequested.connect(self.buttonBMenu)
'''
BUTTON A
'''
@QtCore.pyqtSlot()
def on_buttonA_released(self):
print ('Doing Stuff when clicking on Button A')
def buttonAMenu(self, pos):
menu = QtGui.QMenu()
menu.addAction('First Action', lambda:self.FirstActionButtonA(objects))
menu.addAction('Second Action', lambda:self.SecondActionButtonA(objects))
menu.exec_(QtGui.QCursor.pos())
def FirstActionButtonA(self, objects):
print ('First Action working on :')
print (objects)
def SecondActionButtonA(self):
print ('Second Action working on :')
print (objects)
'''
BUTTON B
'''
@QtCore.pyqtSlot()
def on_buttonB_released(self):
print ('Doing Stuff when clicking on Button B')
def buttonBMenu(self, pos):
menu = QtGui.QMenu()
menu.addAction('First Action', lambda:self.FirstActionButtonB(objects))
menu.addAction('Second Action', lambda:self.SecondActionButtonB(objects))
menu.exec_(QtGui.QCursor.pos())
def FirstActionButtonB(self, objects):
print ('First Action working on :')
print (objects)
def SecondActionButtonB(self):
print ('Second Action working on :')
print (objects)
Sorry, the code highlighting seems to not work...
Thanks a lot for your help !
m.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/cd18e5e1-8b14-4e74-9017-9dac18a6c69a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.