15

I need to create a context menu on right clicking at my window. But I really don't know how to achieve that.

Are there any widgets for that, or I have to create it from the beginning?

Programming language: Python
Graphical lib: Qt (PyQt)

2 Answers 2

42

I can't speak for python, but it's fairly easy in C++.

first after creating the widget you set the policy:

w->setContextMenuPolicy(Qt::CustomContextMenu);

then you connect the context menu event to a slot:

connect(w, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(ctxMenu(const QPoint &)));

Finally, you implement the slot:

void A::ctxMenu(const QPoint &pos) {
    QMenu *menu = new QMenu;
    menu->addAction(tr("Test Item"), this, SLOT(test_slot()));
    menu->exec(w->mapToGlobal(pos));
}

that's how you do it in c++ , shouldn't be too different in the python API.

EDIT: after looking around on google, here's the setup portion of my example in python:

self.w = QWhatever();
self.w.setContextMenuPolicy(Qt.CustomContextMenu)
self.connect(self.w,SIGNAL('customContextMenuRequested(QPoint)'), self.ctxMenu)
4
  • 1
    note that in the PyQt4, CustomContextMenu location in the package is here: PyQt4.QtCore.Qt.CustomContextMenu
    – Jason Coon
    Dec 27, 2010 at 17:08
  • 2
    Gotta love the random downvote after two years and 19 upvotes :-P
    – Evan Teran
    Sep 24, 2011 at 1:48
  • 1
    The downvote was accidental and I'm really really sorry. The answer actually helped me a lot.
    – Vicky T
    Sep 28, 2011 at 22:18
  • @UtkarshSinha See meta.stackexchange.com/questions/127218/… , it should work for downvotes as it does for upvotes. tl;dr: click the arrow again. Dec 15, 2013 at 13:18
15

Another example which shows how to use actions in a toolbar and context menu.

class Foo( QtGui.QWidget ):

    def __init__(self):
        QtGui.QWidget.__init__(self, None)
        mainLayout = QtGui.QVBoxLayout()
        self.setLayout(mainLayout)

        # Toolbar
        toolbar = QtGui.QToolBar()
        mainLayout.addWidget(toolbar)

        # Action are added/created using the toolbar.addAction
        # which creates a QAction, and returns a pointer..
        # .. instead of myAct = new QAction().. toolbar.AddAction(myAct)
        # see also menu.addAction and others
        self.actionAdd = toolbar.addAction("New", self.on_action_add)
        self.actionEdit = toolbar.addAction("Edit", self.on_action_edit)
        self.actionDelete = toolbar.addAction("Delete", self.on_action_delete)
        self.actionDelete.setDisabled(True)

        # Tree
        self.tree = QtGui.QTreeView()
        mainLayout.addWidget(self.tree)
        self.tree.setContextMenuPolicy( Qt.CustomContextMenu )
        self.connect(self.tree, QtCore.SIGNAL('customContextMenuRequested(const QPoint&)'), self.on_context_menu)

        # Popup Menu is not visible, but we add actions from above
        self.popMenu = QtGui.QMenu( self )
        self.popMenu.addAction( self.actionEdit )
        self.popMenu.addAction( self.actionDelete )
        self.popMenu.addSeparator()
        self.popMenu.addAction( self.actionAdd )

    def on_context_menu(self, point):

         self.popMenu.exec_( self.tree.mapToGlobal(point) )
3
  • Thanks @PedroMorgan for this - I found some code online using "_SIGNAL("customContextMenuRequest ... _", and was wondering why it doesn't work :) - thanks to this post, I know now it is called customContextMenuRequested, with "ed" at end... Cheers!
    – sdaau
    Nov 3, 2012 at 11:01
  • I know this is an old post, but the example given by PedroMorgan does not work for me. I have done so many searches on google and this is the closest I have got. All I get when I Right click the list is a little box one pixel in size when I right click. Can anyone point me to a more recent example of using context menus in PyQt which is demonstrated as simply as this one by Pedro. Mar 19, 2013 at 22:59
  • It's so long ago that I cant rememebr where I got working code from, but I did get my code working in the end. Thanks Jan 29, 2014 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.