QGraphicsView::ScrollHandDrag无效问题
创始人
2025-05-31 19:20:38

通过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

相关内容

热门资讯

2025最新版本“wepoke... 你好.wepoker轻量版这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...
第一个攻略教程!全盛棋牌外挂辅... 您好,全盛棋牌这款游戏可以开挂的,确实是有挂的,通过微信【29290261】很多玩家在这款游戏中打牌...
2025最新版本“安卓版AAP... 2025最新版本“安卓版AAPoKer外挂辅助助手“专业师傅带你一起了解更多准备好在安卓版AAPoK...
第一个攻略教程!憨憨娱乐外挂辅... 亲,憨憨娱乐有的,ai轻松简单,又可以获得无穷的乐趣,憨憨娱乐是你和朋友度过闲暇时光的不二选择,赶紧...
第一个攻略教程!川渝互娱外挂辅... 您好,川渝互娱这款游戏可以开挂的,确实是有挂的,通过微信【29290261】很多玩家在这款游戏中打牌...