class MyLabel(QLabel):
def __init__(self,centralwidget):
#centralwidget 窗体参数
super().__init__(centralwidget)
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.rightMenuShow)#开放右键策略
def rightMenuShow(self, point):
#添加右键菜单
self.popMenu = QMenu()
tj=QAction(u'添加', self)
sc=QAction(u'删除', self)
xg = QAction(u'修改', self)
self.popMenu.addAction(tj)
self.popMenu.addAction(sc)
self.popMenu.addAction(xg)
#绑定事件
tj.triggered.connect(self.test)
sc.triggered.connect(self.test)
xg.triggered.connect(self.test)
self.showContextMenu(QtGui.QCursor.pos())
def test(self):
print('test')
def showContextMenu(self, pos):
#调整位置
'''''
右键点击时调用的函数
'''
# 菜单显示前,将它移动到鼠标点击的位置
self.popMenu.move( pos)
self.popMenu.show()
内容暴躁了点,正常现象