C++ 有没有一种方法可以在Qt中以编程方式中断鼠标拖动?
我想保留用户缩放和拖动QGraphicscene的能力,因此我不能简单地锁定QGraphicsView。 但是,用户不能将QGraphicsItem从“场景”视口中拖出。因此,我正在寻找一种在不忽略DragMoveEvent的情况下中断MouseDrageEvent的方法,即让QGraphicsSitem跳回其原点。我曾尝试使用releaseMouse函数来完成此行为,但根本不起作用。有什么建议吗C++ 有没有一种方法可以在Qt中以编程方式中断鼠标拖动?,c++,qt,drag-and-drop,qgraphicsscene,C++,Qt,Drag And Drop,Qgraphicsscene,我想保留用户缩放和拖动QGraphicscene的能力,因此我不能简单地锁定QGraphicsView。 但是,用户不能将QGraphicsItem从“场景”视口中拖出。因此,我正在寻找一种在不忽略DragMoveEvent的情况下中断MouseDrageEvent的方法,即让QGraphicsSitem跳回其原点。我曾尝试使用releaseMouse函数来完成此行为,但根本不起作用。有什么建议吗 谢谢 在处理qt graphics场景视图框架和拖动时,最好重新实现QGraphicsSiteMa
谢谢 在处理qt graphics场景视图框架和拖动时,最好重新实现QGraphicsSiteMand::itemChange,而不是直接处理鼠标 这是在头文件中定义的函数:
protected:
virtual QVariant itemChange( GraphicsItemChange change, const QVariant & value );
然后在函数中,检测位置变化,并根据需要返回新位置
QVariant YourItemItem::itemChange(GraphicsItemChange change, const QVariant & value )
{
if ( change == ItemPositionChange && scene() )
{
QPointF newPos = value.toPointF(); // check if this position is out bound
{
if ( newPos.x() < xmin) newPos.setX(xmin);
if ( newPos.x() > xmax ) newPos.setX(xmax);
if ( newPos.y() < ymin ) newPos.setY(ymin);
if ( newPos.y() > ymax ) newPos.setY(ymax);
return newPos;
}
...
}
诸如此类,您就明白了。我不知道Qt,但请查看Win32 API中的SetCapture和ClipCursor。可能会为此覆盖mouseMoveEvent?将QGraphicsItem从“场景”视口中拖出-QGraphicsSitem位于QGraphicsCenter中,您无法将其拖出“场景”视口。