qt在qcraphicsScene中拖放
qt drag and drop in qgraphicsScene
我在qgraphicsScene上有两个自定义的qgraphicItems,由qgraphicsview渲染。现在,我希望能够将两个项目中的一个拖放到另一个项目。但是,我应该重新考虑哪些事件呢?文档对此有点混淆。
此外,如果用户将qgraphicitem拖到应该放在上面的区域之外的另一个区域,我希望它能跳回原来的位置。
据我所知,这并没有在QGraphicsScene本身中实现。您必须从QGraphicsView或QGraphicsScene派生自己的类,然后重载:
class MyGraphicsView : public QGraphicsView
{
Q_OBJECT;
protected:
virtual void mousePressEvent(QMouseEvent* event);
virtual void mouseMoveEvent(QMouseEvent* event);
virtual void mouseReleaseEvent(QMouseEvent* event);
...
private:
QGraphicsItem *currentDraggedItem;
};
QGraphicsView使用视图/窗口坐标,而QGraphicsScene使用场景坐标。
添加代码,如:
void MyGraphicsView::mousePressEvent(QMouseEvent* event)
{
currentDraggedItem = itemAt(event->pos());
QGraphicsView::mousePressEvent(event);
}
void MyGraphicsView::mouseReleaseEvent(QMouseEvent* event)
{
QGraphicsItem *foundItem = itemAt(event->pos());
if(foundItem && currentDraggedItem &&
foundItem != currentDraggedItem)
{
// Handle DragDrop Here
}
QGraphicsView::mouseReleaseEvent(event);
}
这为一个QGraphicsScene完成了工作。如果您有其中两个,它们必须相互了解,并且必须将坐标从一个QGraphicsView转换到另一个QGraphicsView。正在使用mapTo。。。()。
关键是检查QGraphicsItems rect并查看它们是否相交。
因此,当鼠标按下项目时,存储其当前位置。您现在可以移动它并等待鼠标释放。释放鼠标按钮后,检查项目的边界矩形是否与QRect::contains(const QRectF)相交。如果他们这样做了,那么你就把一个扔到了另一个上面。如果没有,则将图形项设置回先前存储的位置的动画。
只要确保在检查边界矩形是否相交时,在场景空间坐标中使用这两个矩形即可。转换它们,或者使用QGraphicsItem::sceneBoundingRect()。
相关文章:
- 在组合框中拖放项目
- wxWidgets 拖放文件事件处理程序初始化问题(无效static_cast)
- 拖放我的MDI被CRichEditView拦截并在CRichEditView::GetDocument中崩溃 - 如何覆
- 在特定区域拖放 | Qt
- Qt - 在QGraphicScene中拖放时如何从项目中获取文本?
- Qt 模型拖放 - 无法保存类型 QJsonValue
- 如何在 2 个应用程序之间实现拖放?
- 在拖放 QML 列表中插入/删除项目使用 cpp 模型查看
- UI具有拖放功能
- 如何使用拖放功能和点击信号函数QT创建标签
- 使用MFC在对话框上拖放文件
- QListWidget通过setItemWidget使用自定义小部件集进行拖放
- 将拖放连接到QListWidget中的函数的最佳方法
- QTextEdit-根据QCursor位置进行条件拖放
- 如果使用Qt::DecorationRole,则在拖放时崩溃
- 用代码模拟窗口拖放
- Ole 拖放故障排除
- 基于文本的角色扮演游戏拖放和保留物品功能
- 拖放C++在拖放之前操作 Outlook 文件
- 如何在QML无窗口应用程序中设置拖放区域