PyQt5 实现右键菜单

QWidget 及其子类均支持右键菜单功能,通过声明 ContextMenuPolicy 启用菜单

Qt.DefaultContextMenu 默认菜单,重写 contextMenuEvent() 实现自定义
Qt.NoContextMenu 无菜单,事件响应传递给部件父级
Qt.PreventContextMenu 无菜单,事件响应不继续传递
Qt.ActionsContextMenu 事件菜单,只响应部件事件,部件子件的事件不响应
Qt.CustomContextMenu 用户自定义菜单,需绑定事件 customContextMenuRequested,并实现 槽函数
        # self inherit QWidget
        self.setContextMenuPolicy(Qt.DefaultContextMenu)  # contextMenuEvent()
        self.setContextMenuPolicy(Qt.NoContextMenu)
        self.setContextMenuPolicy(Qt.PreventContextMenu)
        self.setContextMenuPolicy(Qt.ActionsContextMenu)
        self.setContextMenuPolicy(Qt.CustomContextMenu)  # customContextMenuRequested()

 

1、实现用户菜单 



class handler_demo(QWidget, Ui_demo):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        # self inherit QWidget
        self.table_widget.setContextMenuPolicy(Qt.CustomContextMenu)
        self.table_widget.customContextMenuRequested.connect(self.custom_right_menu)

    def custom_right_menu(self, pos):
        menu = QMenu()
        opt1 = menu.addAction("menu1")
        opt2 = menu.addAction("menu2")
        action = menu.exec_(self.table_widget.mapToGlobal(pos))
        if action == opt1:
            # do something
            return
        elif action == opt2:
            # do something
            return
        else:
            return


 

2、使用事件菜单 # TODO



class handler_demo(QWidget, Ui_demo):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        # self inherit QWidget
        self.table_widget.setContextMenuPolicy(Qt.ActionsContextMenu)
        self.table_widget.customContextMenuRequested.connect(self.custom_right_menu)

    def custom_right_menu(self, pos):
        menu = QMenu()
        opt1 = menu.addAction("menu1")
        opt2 = menu.addAction("menu2")
        action = menu.exec_(self.table_widget.mapToGlobal(pos))
        if action == opt1:
            # do something
            return
        elif action == opt2:
            # do something
            return
        else:
            return


 

 3、重写默认菜单 # TODO


class handler_demo(QWidget, Ui_demo):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

    def contextMenuEvent(self, event):
        menu = QMenu()
        opt1 = menu.addAction("menu1")
        opt2 = menu.addAction("menu2")
        action = menu.exec_(self.table_widget.mapToGlobal(event.pos()))
        if action == opt1:
            # do something
            return
        elif action == opt2:
            # do something
            return

        menu.move(event.pos())
        menu.show()

 

 参考资料

【1】《QT 实现右键快捷菜单》

 【2】《QT学习窗口右键》

 【3】《Qt.ContextMenuPolicy》,官网

你可能感兴趣的:(程序/Python/概念&分析)