#!/usr/bin/env python
# -*- coding:UTF-8 -*-
# @Time : 2020/9/23 14:54
# @File : ContextMenu.py
# @email : spirit_az@foxmail.com
__author__ = 'ChenLiang.Miao'
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ #
# import --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ #
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import QtCore
# proc function -+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ #
# function main -+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ #
class ContextMenu_A(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ContextMenu_A, self).__init__()
self.createContextMenu()
def createContextMenu(self):
# 打开右键事件
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.showContextMenu)
# 创建QMenu
self.contextMenu = QtWidgets.QMenu(self)
self.addAction = self.contextMenu.addAction(u'action')
self.addAction.triggered.connect(self.actionConnect)
def showContextMenu(self, pos):
"""
:param pos: 传入的为相对坐标 QtGui.QCursor().pos() = pos + self.pos()
:return:
"""
self.contextMenu.move(QtGui.QCursor().pos())
self.contextMenu.show()
def actionConnect(self, *args):
print('ContextMenu')
if __name__ == '__main__':
app = QtWidgets.QApplication([])
ui = ContextMenu_A()
ui.show()
app.exec_()