在QGraphicsView中拖动QGraphicsWidget

如果你的QGraphicsWidget、QGraphicsItem无法相应鼠标、键盘消息,请参考《QGraphicsWidget收不到鼠标、键盘消息解决

两种方法

方法1:

先通过下面的代码设置被拖动想的属性:

setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemSendsGeometryChanges | QGraphicsItem::ItemUsesExtendedStyleOption);

如果你的视图是继承自QGraphicsView,则在重载鼠标移动事件时,基类QGraphicsView的鼠标移动事件请不要屏蔽,否则不能移动项,如下:

void GraphicsView::mouseMoveEvent(QMouseEvent *event)
{
    // 这里根据业务写你需要的代码

    QGraphicsView::mouseMoveEvent(event);  // 这句不能漏掉,否则项不能移动
}

即上面的QGraphicsView::mouseMoveEvent(event); 这句不能漏掉,否则项不能移动。

方法2:

重写鼠标事件

继承QGraphicsItem,重写mousePressEvent、mouseMoveEvent和mouseReleaseEvent三个函数。

void BreakerItem::mousePressEvent( QGraphicsSceneMouseEvent* event )
{
	m_mousePressed=true;
	m_mousePressedPoint=this->scenePos()-event->scenePos();
}

void BreakerItem::mouseMoveEvent( QGraphicsSceneMouseEvent* event )
{
	if(m_mousePressed==true)
	{
		this->setPos(event->scenePos()+m_mousePressedPoint);
	}
} void BreakerItem::mouseReleaseEvent( QGraphicsSceneMouseEvent* event )
{
	m_mousePressed=false;
}

方法2没有亲自测试过,从网上找的。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Qt实现在QGraphicsView拖动QGraphicsItem,你可以按照以下步骤: 1. 创建一个QGraphicsScene实例,并将其设置为QGraphicsView的场景。 2. 在QGraphicsScene创建一个QGraphicsItem。 3. 将QGraphicsItem添加到QGraphicsScene。 4. 重写QGraphicsItem的mousePressEventmouseMoveEvent方法,以便在鼠标按下和移动时更新其位置。 5. 为QGraphicsView启用拖动功能,通过将setDragMode方法设置为ScrollHandDrag来实现。 下面是一个简单的示例代码: ```cpp #include <QtWidgets> class GraphicsItem : public QGraphicsRectItem { public: GraphicsItem(QGraphicsItem* parent = nullptr) : QGraphicsRectItem(parent) { setRect(0, 0, 50, 50); setFlag(QGraphicsItem::ItemIsMovable, true); } protected: void mousePressEvent(QGraphicsSceneMouseEvent* event) override { QGraphicsRectItem::mousePressEvent(event); setCursor(Qt::ClosedHandCursor); } void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override { QGraphicsRectItem::mouseMoveEvent(event); } void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override { QGraphicsRectItem::mouseReleaseEvent(event); setCursor(Qt::OpenHandCursor); } }; int main(int argc, char** argv) { QApplication app(argc, argv); QGraphicsScene scene; QGraphicsView view(&scene); view.setDragMode(QGraphicsView::ScrollHandDrag); GraphicsItem* item = new GraphicsItem; scene.addItem(item); view.setSceneRect(0, 0, 500, 500); view.show(); return app.exec(); } ``` 在这个例子,我们创建了一个自定义的QGraphicsRectItem子类,重写了mousePressEventmouseMoveEvent和mouseReleaseEvent方法。当鼠标按下时,我们将鼠标光标设置为ClosedHandCursor,表示拖动状态开始。当鼠标移动时,QGraphicsRectItem的默认实现会更新其位置。当鼠标释放时,我们将鼠标光标设置为OpenHandCursor,表示拖动状态结束。 我们还创建了一个QGraphicsScene实例,并在其添加了一个GraphicsItem实例。最后,我们将QGraphicsView的场景设置为QGraphicsScene,并启用了拖动功能。 这个例子只是一个简单的演示,你可以根据你的需求进行更改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值