开发者

How to make selection on QGraphicsScene?

开发者 https://www.devze.com 2023-02-12 04:17 出处:网络
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)

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 Recta开发者_StackOverflow社区ngle should be properly drawn? How should I manage mouse events that conflict with each other?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消