出现上下文菜单时如何使小部件接收鼠标发布事件
在Ubuntu20.04上,当Windows可以接收到上下文菜单时,我无法让小部件接收鼠标发布事件。我的PYQT版本是5.15.2。
我已经考虑过手动发送鼠标发布事件,但是我不知道出现上下文菜单时哪个系统会收到鼠标发布事件,并且这样做可能会导致重复发布事件。有更好的解决方案吗?
# coding:utf-8
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMenu, QLabel
class Menu(QMenu):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.addAction('action 1')
self.addAction('action 2')
def showEvent(self, e):
print('show context menu')
super().showEvent(e)
class Widget(QLabel):
def mousePressEvent(self, e):
print('mouse press, button:', e.button())
super().mousePressEvent(e)
def mouseReleaseEvent(self, e):
print('mouse release, button:', e.button())
super().mouseReleaseEvent(e)
class Demo(QWidget):
def __init__(self):
super().__init__()
self.widget = Widget('Click Me', self)
self.widget.move(175, 180)
self.resize(400, 400)
def contextMenuEvent(self, e):
menu = Menu(self)
menu.exec(e.globalPos())
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Demo()
w.show()
app.exec_()
On Ubuntu20.04, I can't have the widget receive the mouse release event when context menu appears while Windows can receive. My pyqt version is 5.15.2.
I've considered sending a mouse release event manually, but I don't know which systems will receive the mouse release event when context menu appears, and doing so may cause repeated release events. Is there any better solution?
# coding:utf-8
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMenu, QLabel
class Menu(QMenu):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.addAction('action 1')
self.addAction('action 2')
def showEvent(self, e):
print('show context menu')
super().showEvent(e)
class Widget(QLabel):
def mousePressEvent(self, e):
print('mouse press, button:', e.button())
super().mousePressEvent(e)
def mouseReleaseEvent(self, e):
print('mouse release, button:', e.button())
super().mouseReleaseEvent(e)
class Demo(QWidget):
def __init__(self):
super().__init__()
self.widget = Widget('Click Me', self)
self.widget.move(175, 180)
self.resize(400, 400)
def contextMenuEvent(self, e):
menu = Menu(self)
menu.exec(e.globalPos())
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Demo()
w.show()
app.exec_()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
一般规则是,当右鼠标按钮在Windows上释放并按在Linux上时,将触发上下文菜单事件。
为了避免不一致并具有忽略OS并始终执行的通用行为,您可以在需要时设置内部标志并进行检查:由于上下文菜单事件可能会优先/em>鼠标按下和上下文菜单。
请记住:
contextmenuevent
必须使用event.ignore()
或通过调用基本实现来明确完成此操作。活动;原因()
IS鼠标
;如果执行“ Undress”部分的功能始终检查上述标志(应该),但实际上并不需要这是不需要的,但是检查是否有完整性和一致性的原因仍然是一个很好的做法;The general rule is that the context menu event is fired when the right mouse buttons is released on Windows and pressed on Linux.
To avoid inconsistency and have a generic behavior that just ignores the OS and always does the same, you can set an internal flag and check it whenever is required: since the context menu event might take precedence, call a function from both mouse press and context menu.
Remember that:
contextMenuEvent
this must be done explicitly either withevent.ignore()
or by calling the base implementation, assuming that it does not accept the event;reason()
isMouse
; this is not actually required if the function that does the "unpress" part always check for the above flag (and it should), but it's still good practice to check for the reason for completeness and consistency;