PySide: adding action to DefaultContextMenu of lineEdit

538 views
Skip to first unread message

Soham Parmar

unread,
Apr 28, 2020, 3:49:07 PM4/28/20
to Python Programming for Autodesk Maya
Hi,
I wanted to add action to my lineEdit rightClick context menu which contains(undo, redo, cut, copy, paste, delete, selectAll)
I just want to add one more action.

I am trying this but its not working.

A_exportCS = QtWidgets.QAction(self.LE_csFilePath)
A_exportCS.setObjectName("A_exportCS")
self.LE_shapesToExport.addAction(A_exportCS)

What am i doing wrong?

Thanks.
Screenshot 2020-04-29 at 1.15.40 AM.png

Justin Israel

unread,
Apr 28, 2020, 4:53:34 PM4/28/20
to python_in...@googlegroups.com
Hi,

There are a couple different modes on a widget for controlling context menus.
The default policy, which you are using in your example, uses the contextMenuEvent on the widget to create the default menu.
The actions mode is the one that will look at the actions() on the widget and create a menu from that. So you are adding an action that isn't being considered. The problem is, if you were to switch to the actions mode then you wouldn't get any of the default actions if they aren't stored in that widget actions list. The goal you have is to augment the default actions menu. In a general case this would be more work because you would need to subclass the widget, reimplement the contextMenuEvent, and defer a callback after the menu has been show so that you can look it up and modify it. Fortunately for QLineEdit we don't have to take that approach, as it provides a method to create the default context menu instance: createStandardContextMenu

Something like this:
class MyLineEdit(QtWidgets.QLineEdit):
    def contextMenuEvent(self, event):
        menu = self.createStandardContextMenu()
        menu.addAction(...)
        menu.exec_(event.globalPos())
Some answers online suggest you could just reimplement createStandardContextMenu to return a modified menu. But createStandardContextMenu is not a virtual method as per the api docs, so it would only work reliably part of the time, when it is being triggered from python as opposed to internally via Qt C++

Justin



--
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_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/d8f30791-89a3-449a-8582-a36cdf309790%40googlegroups.com.

Soham Parmar

unread,
Apr 29, 2020, 9:46:27 AM4/29/20
to Python Programming for Autodesk Maya
Hi,
that solution perfectly worked as i wanted. 
but it's making 2 parts of line edit, on first part it's working fine and on second part, i can't write and no context menu.
I have attached photo that help to understand whats happening.

this is the code
class controlShapes(controlShapesUI.Ui_controlShapes, QtWidgets.QWidget):
def __init__(self, parentWin=getMayaWindow()):
super(controlShapes, self).__init__(parentWin)
self.setupUi(self)

self.LE_csFilePath.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
customContextMenu(self.LE_csFilePath)

class customContextMenu(QtWidgets.QLineEdit):

def contextMenuEvent(self, event):
menu = self.createStandardContextMenu()
      menu.addAction("Export")
menu.exec_(event.globalPos())

Thanks,
Soham.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Screenshot 2020-04-29 at 7.09.31 PM.png

Soham Parmar

unread,
Apr 29, 2020, 3:16:00 PM4/29/20
to Python Programming for Autodesk Maya
Hey, 
so i have solved the problem,
one more line edit was getting created on the top of existing one,

I want to ask can we change the order of contextMenu? my QAction is getting created on bottom, i want to keep it on top.

and i had one more question is i want to create context menu on leftClick on pushButton, same as menuButton in menu bar but on QPushButton. how can we do that?

Justin Israel

unread,
Apr 29, 2020, 3:39:26 PM4/29/20
to python_in...@googlegroups.com


On Thu, Apr 30, 2020, 7:16 AM Soham Parmar <soha...@gmail.com> wrote:
Hey, 
so i have solved the problem,
one more line edit was getting created on the top of existing one,

I want to ask can we change the order of contextMenu? my QAction is getting created on bottom, i want to keep it on top.



and i had one more question is i want to create context menu on leftClick on pushButton, same as menuButton in menu bar but on QPushButton. how can we do that?


Thanks,
Soham.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
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_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b5ab5083-87d3-4d61-8101-37f25609ac38%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages