qgraphicsview鼠标移动图片_QGraphicsView中的QGraphicsItem鼠标拖动

67208c07ae8ef79a1ac5ab10f99b6007.gif

gif录制软件没有录到鼠标

方法1:通过设置QGraphicsTextItem的属性实现(推荐)

QGraphicsScene scene; QGraphicsTextItem *text = scene.addText("Hello, world!"); //接受鼠标拖动 text->setAcceptHoverEvents(true); text->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable); QGraphicsView view(&scene); //鼠标拖动改变位置 view.setDragMode(QGraphicsView::RubberBandDrag); view.show();1

2

3

4

5

6

7

8

9

10

11

方法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;

}1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

文章来源: blog.csdn.net,作者:qq_26480033,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_26480033/article/details/112060526

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值