Issue
I'm trying to create a menu that appears when I click the right mouse button, I've followed some tutorials, but when I put them in my code, and change them, they won't work.
This is my code:
def First(self):
FirstFrame = QtGui.QFrame()
FirstFrame.setFixedSize(230,660)
# LIST VIEW FOLDER
self.folders_lv = QtGui.QTreeView()
self.folders_lv.
# DEFINE THE FUNCTION FOR FIRST FRAME
Firstbox = QtGui.QGridLayout()
Firstbox.addWidget(self.folders_lv,3,0,1,4)
Firstbox.setColumnStretch(1, 1)
FirstFrame.setLayout(Firstbox)
self.folders_lv.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.showMenu())
return FirstFrame
def showMenu(self, pos):
menu= QtGui.QMenu(self)
menu.addAction(QtGui.QAction("Item 1", menu))
menu.addAction(QtGui.QAction("Item 2", menu))
menu.addAction(QtGui.QAction("Item 3", menu))
menu.popup(self.mapToGlobal(pos))
Solution
You need to pass the function object when connecting the signal, so get rid of the parentheses:
self.folders_lv.customContextMenuRequested.connect(self.showMenu)
Also, in the showMenu
slot, you probably want:
menu.popup(self.folders_lv.viewport().mapToGlobal(pos))
Answered By - ekhumoro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.