Qt - 控制拖动行为
假设我希望可拖动小部件的移动方式不同于在拖动时仅停留在光标下方。例如,让小部件仅在一个轴上移动,或者让小部件移动光标与拖动起点之间距离的两倍。我应该重写哪个方法来定义这种行为?
Suppose I want my draggable widget to move differently than just staying under my cursor while being dragged. For instance, having the widget move only in one axis, or have the widget move double the distance between the cursor and the drag starting point. Which method should I override to define this kind of behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你应该问自己一个问题:你是否有一些我想要拖动到特定目标的可拖动元素(例如,你有代表不同形状的小部件,这些小部件应该拖动到代表孔的其他小部件上,并且每个“孔”仅接受相同形状的碎片)或者您只想将一个小部件移动到另一个小部件上?
如果您需要拖放放置功能,在我看来,您无法真正更改鼠标移动期间的行为 - 它或多或少被硬编码到 QDrag 对象中。
如果您只想移动东西,您应该只能通过重新实现鼠标移动/按下/释放事件来做到这一点(您可能还需要通过 QWidget::setMouseTracking 方法启用鼠标跟踪)。
编辑:
这是“只是移动东西”的可拖动标签的示例代码:
现在您只需将其添加到您的主小部件中:
您可以尝试使用 moveMouseEvent 来实现仅在一个轴上拖动。
I think you should ask yourself a question: do you have some draggable elements I want to drag to specific targets (for example you have widgets that represent pieces of different shapes that should be dragged on other widgets representing holes and each "hole" accepts only pieces of the same shape) or do you just want to move one widget over the other widget?
If you need drag & drop functionality, it seems to me that you can't really change the behavior during mouse moving - it's more or less hardcoded into QDrag object.
If you just want to move stuff around, you should be able to do it only be reimplementing mouse Move/Press/Release events (you might also need to enable mouse tracking by QWidget::setMouseTracking method).
EDIT:
This is sample code for draggable label for "just moving stuff around":
Now you just need to add it to your main widget:
You can experiment with moveMouseEvent to achieve dragging only in one axis.
对于小部件,这四种方法控制拖动小部件的行为:
我想您必须重新实现
dragMoveEvent
才能实现您想要的效果。For a widget these four methods control the behaviour of a dragged widget :
I guess you have to reimplement the
dragMoveEvent
to achieve what you want.