菜单多选不关闭
This commit is contained in:
parent
a7c2262577
commit
c213941e7c
4 changed files with 103 additions and 0 deletions
|
@ -102,4 +102,5 @@ encoding//\u81EA\u5B9A\u4E49import/build.py=utf-8
|
||||||
encoding//\u81EA\u5B9A\u4E49import/main.py=utf-8
|
encoding//\u81EA\u5B9A\u4E49import/main.py=utf-8
|
||||||
encoding//\u81EA\u5B9A\u4E49import/src/test.py=utf-8
|
encoding//\u81EA\u5B9A\u4E49import/src/test.py=utf-8
|
||||||
encoding//\u81EA\u5B9A\u4E49\u5C5E\u6027\u6D4B\u8BD5/\u81EA\u5B9A\u4E49\u5C5E\u6027\u6D4B\u8BD5.py=utf-8
|
encoding//\u81EA\u5B9A\u4E49\u5C5E\u6027\u6D4B\u8BD5/\u81EA\u5B9A\u4E49\u5C5E\u6027\u6D4B\u8BD5.py=utf-8
|
||||||
|
encoding//\u83DC\u5355/\u83DC\u5355\u591A\u9009\u4E0D\u5173\u95ED.py=utf-8
|
||||||
encoding//\u8868\u683C\u590D\u5236/TableView.py=utf-8
|
encoding//\u8868\u683C\u590D\u5236/TableView.py=utf-8
|
||||||
|
|
23
菜单/README.md
Normal file
23
菜单/README.md
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
# 菜单设置多选并且不关闭
|
||||||
|
|
||||||
|
### [Python3.4.4 or Python3.5][PyQt5]
|
||||||
|
|
||||||
|
有时候会遇到这种需求:在界面某个位置弹出一个菜单,其中里面的菜单项可以多选(类似配置选项),
|
||||||
|
此时用QMenu会遇到点击一个菜单项就会自动关闭,当然可以通过其他方式实现该功能,
|
||||||
|
不过这里就采用QMenu通过特殊的方式来实现该需求。
|
||||||
|
|
||||||
|
要实现的效果:<br/>
|
||||||
|
菜单1<br/>
|
||||||
|
菜单2<br/>
|
||||||
|
菜单3<br/>
|
||||||
|
菜单4<br/>
|
||||||
|
|
||||||
|
点击菜单1、2、3可以多选不关闭菜单<br/>
|
||||||
|
点击菜单4可以勾选,并且关闭菜单
|
||||||
|
|
||||||
|
知识点:<br/>
|
||||||
|
1、设置菜单项可勾选:通过QAction.setCheckable(True)方法实现<br/>
|
||||||
|
2、设置菜单不可关闭:通过覆盖QMenu的鼠标释放mouseReleaseEvent方法(可直接替换或者通过installEventFilter安装事件过滤器实现)
|
||||||
|
|
||||||
|
# 截图
|
||||||
|
![截图1](ScreenShot/1.gif)
|
BIN
菜单/ScreenShot/1.gif
Normal file
BIN
菜单/ScreenShot/1.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 136 KiB |
79
菜单/菜单多选不关闭.py
Normal file
79
菜单/菜单多选不关闭.py
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
"""
|
||||||
|
Created on 2018年10月24日
|
||||||
|
@author: Irony
|
||||||
|
@site: https://github.com/892768447
|
||||||
|
@email: 892768447@qq.com
|
||||||
|
@file: 菜单多选不关闭
|
||||||
|
@description:
|
||||||
|
"""
|
||||||
|
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QPushButton, QMenu,\
|
||||||
|
QAction
|
||||||
|
|
||||||
|
|
||||||
|
__Author__ = """By: Irony
|
||||||
|
QQ: 892768447
|
||||||
|
Email: 892768447@qq.com"""
|
||||||
|
__Copyright__ = "Copyright (c) 2018 Irony"
|
||||||
|
__Version__ = "Version 1.0"
|
||||||
|
|
||||||
|
|
||||||
|
class Window(QWidget):
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(Window, self).__init__(*args, **kwargs)
|
||||||
|
layout = QVBoxLayout(self)
|
||||||
|
self.labelInfo = QLabel(self)
|
||||||
|
self.button = QPushButton('带按钮的菜单', self)
|
||||||
|
layout.addWidget(self.labelInfo)
|
||||||
|
layout.addWidget(self.button)
|
||||||
|
|
||||||
|
# 添加菜单
|
||||||
|
self._initMenu()
|
||||||
|
|
||||||
|
def _initMenu(self):
|
||||||
|
# 创建菜单
|
||||||
|
self._menu = QMenu(self.button)
|
||||||
|
# 替换menu的鼠标释放事件达到选择性不关闭菜单
|
||||||
|
self._menu.mouseReleaseEvent = self._menu_mouseReleaseEvent
|
||||||
|
self._menu.addAction('菜单1', self._checkAction)
|
||||||
|
self._menu.addAction('菜单2', self._checkAction)
|
||||||
|
self._menu.addAction(
|
||||||
|
QAction('菜单3', self._menu, triggered=self._checkAction))
|
||||||
|
action = QAction('菜单4', self._menu, triggered=self._checkAction)
|
||||||
|
# 添加自定义的属性,判断该属性可以关闭菜单
|
||||||
|
action.setProperty('canHide', True)
|
||||||
|
self._menu.addAction(action)
|
||||||
|
for action in self._menu.actions():
|
||||||
|
# 循环设置可勾选
|
||||||
|
action.setCheckable(True)
|
||||||
|
self.button.setMenu(self._menu)
|
||||||
|
|
||||||
|
def _menu_mouseReleaseEvent(self, event):
|
||||||
|
action = self._menu.actionAt(event.pos())
|
||||||
|
if not action:
|
||||||
|
# 没有找到action就交给QMenu自己处理
|
||||||
|
return QMenu.mouseReleaseEvent(self._menu, event)
|
||||||
|
if action.property('canHide'): # 如果有该属性则给菜单自己处理
|
||||||
|
return QMenu.mouseReleaseEvent(self._menu, event)
|
||||||
|
# 找到了QAction则只触发Action
|
||||||
|
action.activate(action.Trigger)
|
||||||
|
|
||||||
|
def _checkAction(self):
|
||||||
|
# 三个action都响应该函数
|
||||||
|
self.labelInfo.setText('\n'.join(['{}\t选中:{}'.format(
|
||||||
|
action.text(), action.isChecked()) for action in self._menu.actions()]))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import sys
|
||||||
|
import cgitb
|
||||||
|
sys.excepthook = cgitb.enable(1, None, 5, 'text')
|
||||||
|
from PyQt5.QtWidgets import QApplication
|
||||||
|
app = QApplication(sys.argv)
|
||||||
|
w = Window()
|
||||||
|
w.resize(400, 400)
|
||||||
|
w.show()
|
||||||
|
sys.exit(app.exec_())
|
Loading…
Reference in a new issue