如何在QGraphicsScene上进行选择?
我正在使用图形视图框架在 Qt 中编写一个图表编辑器。 目前我只是想在 GraphScene (QGraphicsScene) 上创建一个简单的节点 (QGraphicsItem)。我创建了一个画布(QGraphicsView)并向其添加了一个网格。我什至可以添加节点并在场景中移动它们。我的最终目标是拥有不同的工作模式、编辑和样式选项等。现在我只想知道如何为场景中已有的节点设置选择。我尝试使用鼠标事件执行此操作,但注意到事件调用选择和节点插入重叠...当我尝试选择某些内容时,会创建一个新节点...这是我的第一个 Qt 应用程序,所以我不完全理解如何我想要实现的功能应该被设计。
应如何正确绘制选择矩形? 我应该如何管理相互冲突的鼠标事件?
I'm writing a diagram editor in Qt with Graphics View Framework.
Currently I'm just trying to create a simple Node (QGraphicsItem) on GraphScene (QGraphicsScene). I created a Canvas (QGraphicsView) and added a grid to it. I can even add Nodes and move them around on scene. My final goal is to have different working modes, editing and styling options, etc. For now I just want to know how can I setup selection for Nodes already present on scene. I tried doing it with mouse events but noticed that event calls for selection and Node insertion overlap... When I try to select something a new Node is created... This is my first Qt application so I don't fully understand how the functionality I want to achieve should be designed.
How the Selection Rectangle should be properly drawn?
How should I manage mouse events that conflict with each other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
希望这有帮助。
Hope this helps.
如果鼠标事件重叠,您应该有一个地方(例如 QGraphicsView 来处理鼠标单击/移动事件)并创建一个状态机,然后根据您所处的状态处理事件。您需要规划您的架构嗯,这可能是一个非常复杂的任务。
就像这两个 NodeEditor 教程 #11 和 #12 中所示: https://www.youtube.com/watch?v=pk4v2xuXlm4 和 https://www.youtube.com/watch?v=-VYcQojkloE)
如果您仍想了解更深入的解释Qt 的功能和事件,这里是一个完整的教程列表,其中实现了所有可能的功能,例如将边缘拖动到节点、选择它们并删除它们、剪切边缘、序列化等,您可以查看 50 个完整列表我在这里录制的教程: https://www.blenderfreak.com/教程/节点编辑器教程系列/。
我放置了整个列表的链接,因为不可能在此页面上编写所有代码......
In case of the overlapping mouse events, you should have a single place (like QGraphicsView to handle the mouse clicking/moving events) and create a state machine and then handle the events according to the state you are in. You need to plan your architecture well and that can be really complex task.
Like it's shown in these two NodeEditor tutorials #11 and #12: https://www.youtube.com/watch?v=pk4v2xuXlm4 and https://www.youtube.com/watch?v=-VYcQojkloE)
If you still want more in depth explanation of the functionality and events of Qt, here is a full list of tutorials with implementing all possible features like dragging edges to nodes, selecting them and deleting them, cutting edges, serialization, etc., you can have a look on the whole list of 50 tutorials I've recorded here: https://www.blenderfreak.com/tutorials/node-editor-tutorial-series/.
I'm putting a link to the whole list, since it's not possible to write all the code on this page...