@@ -114,10 +114,56 @@ def initUI(self):
114114 self .reset_button .setFixedHeight (45 )
115115 self .reset_button .clicked .connect (lambda : self .reset_count ())
116116
117+ # 自定义的鼠标事件处理方法,将右键事件转换为左键事件
118+ def custom_mouse_press_event (widget , event ):
119+ if event .button () == Qt .MouseButton .RightButton :
120+ # 将右键按下事件转换为左键按下事件
121+ new_event = QMouseEvent (
122+ QEvent .Type .MouseButtonPress ,
123+ event .position (),
124+ Qt .MouseButton .LeftButton ,
125+ Qt .MouseButton .LeftButton ,
126+ Qt .KeyboardModifier .NoModifier ,
127+ )
128+ QApplication .sendEvent (widget , new_event )
129+ else :
130+ # 其他事件正常处理
131+ PushButton .mousePressEvent (widget , event )
132+
133+ def custom_mouse_release_event (widget , event ):
134+ if event .button () == Qt .MouseButton .RightButton :
135+ # 将右键释放事件转换为左键释放事件
136+ new_event = QMouseEvent (
137+ QEvent .Type .MouseButtonRelease ,
138+ event .position (),
139+ Qt .MouseButton .LeftButton ,
140+ Qt .MouseButton .NoButton ,
141+ Qt .KeyboardModifier .NoModifier ,
142+ )
143+ QApplication .sendEvent (widget , new_event )
144+ else :
145+ # 其他事件正常处理
146+ PushButton .mouseReleaseEvent (widget , event )
147+
117148 self .minus_button = PushButton ("-" )
118149 self ._set_widget_font (self .minus_button , 20 )
119150 self .minus_button .setFixedSize (45 , 45 )
120151 self .minus_button .clicked .connect (lambda : self .update_count (- 1 ))
152+ # 禁用右键菜单,兼容触屏
153+ self .minus_button .setContextMenuPolicy (Qt .ContextMenuPolicy .NoContextMenu )
154+
155+ # 动态替换按钮的鼠标事件处理方法
156+ original_minus_press = self .minus_button .mousePressEvent
157+ original_minus_release = self .minus_button .mouseReleaseEvent
158+
159+ def minus_press_wrapper (event ):
160+ custom_mouse_press_event (self .minus_button , event )
161+
162+ def minus_release_wrapper (event ):
163+ custom_mouse_release_event (self .minus_button , event )
164+
165+ self .minus_button .mousePressEvent = minus_press_wrapper
166+ self .minus_button .mouseReleaseEvent = minus_release_wrapper
121167
122168 # 添加长按连续减功能
123169 self .minus_button .pressed .connect (lambda : self .start_long_press (- 1 ))
@@ -133,6 +179,21 @@ def initUI(self):
133179 self ._set_widget_font (self .plus_button , 20 )
134180 self .plus_button .setFixedSize (45 , 45 )
135181 self .plus_button .clicked .connect (lambda : self .update_count (1 ))
182+ # 禁用右键菜单,兼容触屏
183+ self .plus_button .setContextMenuPolicy (Qt .ContextMenuPolicy .NoContextMenu )
184+
185+ # 动态替换按钮的鼠标事件处理方法
186+ original_plus_press = self .plus_button .mousePressEvent
187+ original_plus_release = self .plus_button .mouseReleaseEvent
188+
189+ def plus_press_wrapper (event ):
190+ custom_mouse_press_event (self .plus_button , event )
191+
192+ def plus_release_wrapper (event ):
193+ custom_mouse_release_event (self .plus_button , event )
194+
195+ self .plus_button .mousePressEvent = plus_press_wrapper
196+ self .plus_button .mouseReleaseEvent = plus_release_wrapper
136197
137198 # 添加长按连续加功能
138199 self .plus_button .pressed .connect (lambda : self .start_long_press (1 ))
@@ -1165,6 +1226,15 @@ def clearLayout(self, layout):
11651226 if child .widget ():
11661227 child .widget ().setParent (None )
11671228
1229+ def eventFilter (self , obj , event ):
1230+ """事件过滤器,处理触屏长按事件"""
1231+ if obj in (self .minus_button , self .plus_button ):
1232+ if event .type () == QEvent .Type .MouseButtonPress :
1233+ # 处理左/右键点击,确保长按功能正常
1234+ return super ().eventFilter (obj , event )
1235+ # 其他事件正常处理
1236+ return super ().eventFilter (obj , event )
1237+
11681238 def _set_widget_font (self , widget , font_size ):
11691239 """为控件设置字体"""
11701240 # 确保字体大小有效
0 commit comments