如何在QGraphicsScene上进行选择?

发布于 2024-10-18 13:27:49 字数 337 浏览 7 评论 0原文

我正在使用图形视图框架在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归属感 2024-10-25 13:27:49
  • 您可以使用可检查按钮/操作(即可检查属性设置为“true”的 QPushButton/QAction)在“编辑”和“编辑”之间切换。插入模式。然后,检查鼠标事件中的状态,并仅在处于插入模式时插入新项目。
  • 您还可以区分鼠标按钮 - 例如,使用右键拖动时插入项目
  • 或使用 QKeyboardModifiers - 例如:拖动 + Ctrl 时 - 插入项目。

希望这有帮助。

  • You can use a checkable button/action(that's a QPushButton/QAction with a checkable property set to 'true) to switch between Edit & Insert mode. Then you check the state in your mouse event and insert a new item only if you're in Insertion mode.
  • You can also distinct between mouse buttons - insert item when dragged with the right button for example
  • Or use QKeyboardModifiers - for example: on drag + Ctrl - insert item.

Hope this helps.

暮年 2024-10-25 13:27:49

如果鼠标事件重叠,您应该有一个地方(例如 QGraphicsView 来处理鼠标单击/移动事件)并创建一个状态机,然后根据您所处的状态处理事件。您需要规划您的架构嗯,这可能是一个非常复杂的任务。

  • 设置状态枚举/常量,
  • 在 if 条件中引用事件中的当前机器状态,
  • 将“业务逻辑”保留在一个位置,

就像这两个 NodeEditor 教程 #11 和 #12 中所示: https://www.youtube.com/watch?v=pk4v2xuXlm4https://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.

  • set your state enum/constants
  • refer to the current machine state in your events in your if conditions
  • keep your "business logic" on a single place

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文