如果按住鼠标按钮,如何强制 mousePressEvent 重新触发?
我正在编写一个小部件,当按下鼠标按钮时,它会执行一个操作。如果鼠标按钮未释放,我需要重复它。实现这一目标的正确方法是什么?
目前我正在使用单次计时器在按下时重复,但这似乎不是正确的“QT”方式。
I have a widget I am writing and when the mouse button is pressed it takes an action. I need it to repeat if the mouse-button is not released. What is the proper way to achieve this?
Currently I am using a single-shot timer to repeat while pressed, but it seems like this isn't the proper 'QT' way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的方法似乎是一种合理的方法,因为 Qt API 似乎没有适当的内置事件。
Your method seems like a reasonable one since it doesn't appear that the Qt API has an appropriate built-in event.
如果你的小部件就像一个 UI 按钮(我能想到的大多数用途都是像按钮一样的小部件),你可以继承
QAbstractButton
。您仍然需要提供自己的绘图,但此小部件提供了 autoRepeat 属性(以及关联的调整参数)重复发出单击信号。然而,它似乎并没有触发事件处理程序。或者,您可以查看该类的源代码,了解它们自己是如何实现的。
If your widget is acting like a UI button (and most uses I can think of for this are for widgets acting like a button), you could inherit from
QAbstractButton
. You would still need to provide your own drawing, but this widget provides an autoRepeat property (and associated tuning parameters) to repeatedly emit the clicked signal if the user holds down the mouse button over the widget. It does not appear, however, that it triggers the event handlers.Alternately, you could check out the source code for the class to see how they implement it themselves.