PyQt5 右键菜单Context Menu 弹出的方法

右键菜单Context Menu 弹出的方法

python 官网相关介绍 https://wiki.python.org/moin/PyQt/Handling context menus

方法一
子类化控件,重新实现 contextMenuEvent() 方法

$ cat subclass2_1.py 
#!/usr/bin/env python
# coding=utf-8
import os
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sip
 
 
class MyWindow(QDialog):
 
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.layout = QVBoxLayout()
        self.widget1 = QListWidget()
        self.widget1.setWindowTitle('aaaaaa')
        self.layout.addWidget(self.widget1)
        self.widget1.resize(300,200)
        self.widget2 = QListWidget()
        self.widget2.setWindowTitle('bbbbbb')
        self.widget2.resize(300,200)
        self.layout.addWidget(self.widget2)
        self._diyWindow = DiyWindow()
        self.layout.addWidget(self._diyWindow)
        self.setLayout(self.layout)
        #self.resize(300,200)
class DiyWindow(QDialog):
    def __init__(self, parent=None):
        super(DiyWindow, self).__init__(parent)
        self.layout = QVBoxLayout()
        self.widget = QListWidget()
        self.layout.addWidget(self.widget)
        self.setLayout(self.layout)
        self.widget.resize(300,200)
    # 当按右键的时候,这个event会被触发
    def contextMenuEvent(self, event):
 
        menu = QMenu(self)
        quitAction = menu.addAction("Quit")
        action = menu.exec_(self.mapToGlobal(event.pos()))
        if action == quitAction:
            qApp.quit()
 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())

run 一下,看运行的结果,按照上面的代码,只有第三个框内点击右键,才会弹出菜单栏,所以实际上我们可以把这个功能实现在不同的区域。
PyQt5 右键菜单Context Menu 弹出的方法_第1张图片
注意一下:
exec():
显示一个模式对话框,并且锁住程序直到用户关闭该对话框为止。函数返回一个DialogCode结果。
在对话框弹出期间,用户不可以切换同程序下的其它窗口,直到该对话框被关闭。
原文:Shows the dialog as a modal dialog, blocking until the user closes it. The function returns a DialogCode result.
Users cannot interact with any other window in the same application until they close the dialog.
父类实例化子类 直接用.show()方法闪退, 所以要实例化为父类的全局变量 or 执行exec_() 方法

来源
作者:刘月玮
链接:https://www.jianshu.com/p/dade4bea67b2

方法二

对 widget 添加actions, 把 context menu policy 设成 ActionsContextMenu.

$ cat actions2.py 
#!/usr/bin/env python
# coding=utf-8
import os
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

import sip
 
 
class MyWindow(QDialog):
 
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.layout = QVBoxLayout()
        self.widget = QListWidget()
        self.layout.addWidget(self.widget)
        self.setLayout(self.layout)
        self.resize(300,200)
         
        self.setContextMenuPolicy(Qt.ActionsContextMenu) #Qt 
        # 直接把action加到自身上
        quitAction = QAction("Quit", self)
        quitAction.triggered.connect(qApp.quit)
        self.addAction(quitAction)
 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())

右键后弹出 action
PyQt5 右键菜单Context Menu 弹出的方法_第2张图片

方法三
自定义弹出菜单

重设右键并绑定信号槽

$ cat menubar1.py 
#!/usr/bin/env python
# coding=utf-8
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
#from PyQt5 import QtWidgets

class MenuDemo(QMainWindow):
    def __init__(self,parent=None):
        super(MenuDemo, self).__init__(parent)

        self.menuBar1 = self.menuBar()
        self.menuBar1.setGeometry(QRect(0, 0, 606, 26))
        self.menuBar1.setObjectName("menuBar")
        self.menuBar1.addMenu('File')

        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested['QPoint'].connect(self.rightMenuShow)

    def rightMenuShow(self):
        rightMenu = QMenu(self.menuBar1)
        
        self.actionreboot = QAction('zhangji')
        self.actionreboot.setObjectName("actionreboot")
        self.actionreboot.setText(QCoreApplication.translate("MainWindow", "重新开机"))
        rightMenu.addAction(self.actionreboot)
        
        rightMenu.exec_(QCursor.pos())

if __name__ == '__main__':
    app=QApplication(sys.argv)
    demo=MenuDemo()
    demo.show()
    sys.exit(app.exec_())

run一下
PyQt5 右键菜单Context Menu 弹出的方法_第3张图片
再看一个例子

#!/usr/bin/env python
# coding=utf-8
import os
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sip
 
 
class MyWindow(QDialog):
 
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.layout = QVBoxLayout()
        self.widget = QListWidget()
        self.layout.addWidget(self.widget)
        self.setLayout(self.layout)
        self.resize(300, 200)
 
        self.widget.setContextMenuPolicy(Qt.CustomContextMenu)
        self.widget.customContextMenuRequested.connect(self.openMenu)
 
    def openMenu(self, position):
 
        menu = QMenu()
        quitAction = menu.addAction("Quit")
        action = menu.exec_(self.widget.mapToGlobal(position))
        if action == quitAction:
            qApp.quit()
 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())

PyQt5 右键菜单Context Menu 弹出的方法_第4张图片
其他相关的程序,也可以这样设置
PyQt5 右键菜单Context Menu 弹出的方法_第5张图片

你可能感兴趣的:(Qt)