Fetch the repository succeeded.
This action will force synchronization from PyQt5/PyQt, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年10月24日
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: 892768447@qq.com
@file: MultiSelect
@description:
"""
try:
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton, QMenu, QAction
except ImportError:
from PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton, QMenu, QAction
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
cgitb.enable(format='text')
app = QApplication(sys.argv)
w = Window()
w.resize(400, 400)
w.show()
sys.exit(app.exec_())
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。