【问题标题】:Is there a way to interrupt a mouse dragging programmatically in Qt?有没有办法在 Qt 中以编程方式中断鼠标拖动?
【发布时间】:2017-02-14 00:13:20
【问题描述】:

我想保留用户缩放和拖动QGraphicsScene 的能力,因此我不能简单地锁定QGraphicsView。 然而,用户不应该能够将QGraphicsItem 拖出场景视口。因此,我正在寻找一种在不忽略 DragMoveEvent 的情况下中断 MouseDragEvent 的方法(也就是让 QGraphicsItem 跳回其原点)。我试图使用releaseMouse()-function 来完成这种行为,但这根本不起作用。有什么建议吗?

谢谢!

【问题讨论】:

  • 我不了解 Qt,但请查看 Win32 API 中的 SetCapture 和 ClipCursor。
  • stackoverflow.com/questions/11172420/moving-object-with-mouse 可能会为此目的覆盖 mouseMoveEvent?
  • 将 QGraphicsItem 拖出场景视口 - QGraphicsItem 存在于 QGraphicsScene 中,您不能将其拖出场景视口。

标签: c++ qt drag-and-drop qgraphicsscene


【解决方案1】:

在处理qt图形场景视图框架和拖动时,最好重新实现QGraphicsItemand::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;
    }

   ...
}

类似的东西,你懂的。

【讨论】:

    猜你喜欢
    • 2017-09-22
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    相关资源
    最近更新 更多