Skip to content

Commit 38ac5bd

Browse files
committed
fix:翻译弹窗的右键事件正常冒泡
1 parent bef3fa3 commit 38ac5bd

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

config.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ copy_triggered_hotkey = ctrl+shift+d,ctrl+8
1212
copy_into_english_triggered_hotkey = alt+c,alt+v
1313
; 停用/启用复制翻译的快捷键,可以设置多个,逗号分隔
1414
stoptrans_triggered_hotkey = ctrl+1,f8
15-
; 悬浮窗的文本内容改为可复制的快捷键,可以设置多个,逗号分隔
15+
; (废弃,改为右键)悬浮窗的文本内容改为可复制的快捷键,可以设置多个,逗号分隔
1616
text_selectable_triggered_hotkey = a,z,space

main.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,34 @@ def copy_selected_text():
4848
"""模拟 Ctrl+C 并返回复制的文本"""
4949
if not wait_for_all_keys_up():
5050
return None
51-
5251
# 模拟 Ctrl+C
5352
keyboard.press('ctrl')
5453
keyboard.press('c')
5554
time.sleep(0.1) # 等待复制完成
5655
keyboard.release('c')
5756
keyboard.release('ctrl')
58-
5957
# 获取剪贴板内容
6058
return pyperclip.paste()
6159

60+
class RightClickIgnoreLabel(QLabel):
61+
# 不处理右键鼠标事件的Qlabel
62+
def __init__(self, *args, **kwargs):
63+
super().__init__(*args, **kwargs)
64+
def mousePressEvent(self, event):
65+
if event.button() == Qt.RightButton:
66+
# 忽略右键事件,让其传递给父控件
67+
event.ignore()
68+
else:
69+
# 其他按钮(如左键)使用默认处理方式
70+
super().mousePressEvent(event)
71+
def mouseReleaseEvent(self, event):
72+
if event.button() == Qt.RightButton:
73+
event.ignore()
74+
else:
75+
super().mouseReleaseEvent(event)
76+
def contextMenuEvent(self, event):
77+
# 忽略上下文菜单事件,让其传递给父控件
78+
event.ignore()
6279

6380
# 翻译弹窗,用于展示翻译结果
6481
class TextWindow(QWidget):
@@ -83,7 +100,8 @@ def setup_ui(self):
83100

84101
layout = QVBoxLayout()
85102
layout.setContentsMargins(0, 0, 0, 0)
86-
self.content = QLabel(self)
103+
self.content = RightClickIgnoreLabel(self)
104+
self.content.setContextMenuPolicy(Qt.NoContextMenu)
87105
if self.position:
88106
pad = min(self.position.width(), self.position.height())
89107
pad = int(pad / 10)

0 commit comments

Comments
 (0)