通过QGraphicsView::setDragMode(QGraphicsView::ScrollHandDrag)
实现鼠标拖动视图,但在以下情况是无效的(即视图不会移动):鼠标事件正在被某一个item处理。如果item通过QGraphicsItem::setFlags
设置了非0值(如QGraphicsItem::ItemIsSelectable)并且选中了item,鼠标事件会被item截获。
QGraphicsView::dragMode
This property holds the behavior for dragging the mouse over the scene while the left mouse button is pressed.
This property defines what should happen when the user clicks on the scene background and drags the mouse (e.g., scrolling the viewport contents using a pointing hand cursor, or selecting multiple items with a rubber band). The default value, NoDrag, does nothing.
This behavior only affects mouse clicks that are not handled by any item. You can define a custom behavior by creating a subclass of QGraphicsView and reimplementing mouseMoveEvent().
该行为只有在鼠标点击事件没有被任何item处理的时候生效。实现QGraphicsView::mouseMoveEvent()以改变此行为
QGraphicsScene::mouseGrabberItem()
Returns the current mouse grabber item, or nullptr if no item is currently grabbing the mouse. The mouse grabber item is the item that receives all mouse events sent to the scene.
An item becomes a mouse grabber when it receives and accepts a mouse press event, and it stays the mouse grabber until either of the following events occur:
(1) If the item receives a mouse release event when there are no other buttons pressed, it loses the mouse grab.
(2) If the item becomes invisible (i.e., someone calls item->setVisible(false)), or if it becomes disabled (i.e., someone calls item->setEnabled(false)), it loses the mouse grab.
(3) If the item is removed from the scene, it loses the mouse grab.
If the item loses its mouse grab, the scene will ignore all mouse events until a new item grabs the mouse (i.e., until a new item receives a mouse press event).
QGraphicsItem::grabMouse()
Grabs the mouse input.
This item will receive all mouse events for the scene until any of the following events occurs:
(1) The item becomes invisible
(2) The item is removed from the scene
(3) The item is deleted
(4) The item call ungrabMouse()
(5) Another item calls grabMouse(); the item will regain the mouse grab when the other item calls ungrabMouse().
When an item gains the mouse grab, it receives a QEvent::GrabMouse event. When it loses the mouse grab, it receives a QEvent::UngrabMouse event. These events can be used to detect when your item gains or loses the mouse grab through other means than receiving mouse button events.
It is almost never necessary to explicitly grab the mouse in Qt, as Qt grabs and releases it sensibly. In particular, Qt grabs the mouse when you press a mouse button, and keeps the mouse grabbed until you release the last mouse button. Also, Qt::Popup widgets implicitly call grabMouse() when shown, and ungrabMouse() when hidden.
Note that only visible items can grab mouse input. Calling grabMouse() on an invisible item has no effect.
Keyboard events are not affected.
通过点击视图中的空白区域就可以拖动视图,前提是空白区域本身不要存在QGraphicsItem::flags大于0的item
上一篇:共享理论中至二人麻将辅助挂神器(辅助挂),有辅助猫腻了(2023已更新)-哔哩哔哩
下一篇:解决 elementUI 的 table 表格改变数据不更新问题 解决 vue数据不更新问题 element-ui 表格数据不更新