有没有办法在Qt中以编程方式中断鼠标拖动
Is there a way to interrupt a mouse dragging programmatically in Qt?
我想保留用户缩放和拖动QGraphicsScene
的能力,因此我不能简单地锁定QGraphicsView
。但是,用户不应能够将QGraphicsItem
拖出场景视口。因此,我正在寻找一种方法来中断MouseDragEvent而不忽略DragMoveEvent(也就是QGraphicsItem
跳回到其原点(。我试图使用 releaseMouse()
-函数完成此行为,但这根本不起作用。有什么建议吗?
谢谢!
在处理 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;
}
...
}
像这样的东西,你明白了。
相关文章:
- 使用 Gtkmm 以编程方式选择 Gtk::TextView 中的文本
- 在没有管理员权限的情况下,在 c++ 中以编程方式将程序添加到启动
- 如何以编程方式将音频从任何录制设备路由到任何播放设备
- 我是否可以使用 win32 句柄以编程方式记录发送到/接收到 USB/COM 的内容
- 如何以编程方式使窗口全屏显示?
- 终端如何在 shell 提示符下以编程方式阻止退格,以便您无法在其上退格,例如 $ 表示 bash 或 C:\blah\>
- 如何使用打印到 PDF 打印机在 MFC 和 CView 中以编程方式打印而不提示输入文件名?
- 如何以编程方式显示功能区按钮的下拉菜单?
- 是否可以以编程方式构造 std::initializer_list?
- 在 Visual C++ 中以编程方式更改按钮的背景图像
- 附加调试器并以编程方式获取变量地址 Visual Studio
- 以编程方式重新连接断开的 VHD 链
- 如何以编程方式获取任务管理器进程CPU使用率(不是PerfMon API)
- 以编程方式在任何按钮上突出显示 Qt 鼠标悬停按钮
- 以编程方式防止重命名或删除文件,但仍使其可写
- 以编程方式连接子图
- 如何以编程方式区分在内存的堆和堆栈部分创建的对象
- 如何以编程方式从PKCS7中提取CA、多个CA和公共证书/密钥
- 如何以编程方式将路由添加到网络接口
- 以编程方式在 C++ 中创建函数