QGraphicsView 更改鼠标样式 以及QGraphicsItem悬停时更改鼠标样式

一个编辑区域,用QGraphi参数View写的,可以放大,鼠标按下后可以拖拽查看,这个时候希望鼠标可以是"小手"抓取的样子.QGraphicsView上有一些个QGraphicsItem,希望鼠标悬停在item上时可以变成四向箭头,然后可以拉伸item.

所以重新了QGraphicsView的

void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);

三个方法,并在其中写了判断,

if (m_bDrag)
{
this->setCursor(Qt::OpenHandCursor);
}

QGraphicsItem重写了

void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void hoverMoveEvent(QGraphicsSceneHoverEvent *event);

方法,并且也在其中添加了判断,

if (m_bHover)
{
this->setCursor(Qt::SizeAllCursor);
}

但是在运行的时候并没有按照预想的执行,当QGraphicsView中未添加Item的时候,可以显示"小手"的拖拽光标,也可以进行图标切换,但是当添加过item后,

虽然也执行了view中改变鼠标样式的函数,但是并没有效果,当鼠标悬停在Item上方的时候,鼠标样式发生改变.

分析了一下,this->setCursor(Qt::OpenHandCursor);和this->setCursor(Qt::SizeAllCursor);的this并不是一个对象,this是View对象,而this是item对象,

所以在Item的悬停方法中,我改成这样

void MytItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
if (m_bHover)
{

// 获取当前item所属于的scene,然后获取scene的Views,因为我只有一个View,所以用下标at(0)获取了view

QGraphicsView * view = this->scene()->views().at(0);

// 然后用view设置鼠标样式
view->setCursor(Qt::SizeAllCursor);
}
QGraphicsItem::hoverMoveEvent(event);
}

问题解决,在查找的时候,发现有一篇博客这样写,但是还未验证

解决方法就是在QGraphicsView子类中使用viewport()->setCursor()而不是直接的setCursor(),这样才能真正改变视觉上的鼠标形状。viewport()函数定义在QAbstractScrollArea类中,QGraphicsView继承自QAbstractScrollArea类,对于更新更新视图内容应该用viewport()->update(),而不是直接用update()。

http://blog.csdn.net/afterward___/article/details/46408355


  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt 中,QGraphicsItemQGraphicsView 都有鼠标事件处理函数,分别是 QGraphicsItem::mousePressEvent()、QGraphicsView::mousePressEvent() 等。如果你想在 QGraphicsItem 处理完鼠标事件后让 QGraphicsView 继续处理,可以使用 QGraphicsView::mousePressEvent() 函数的 callSuper() 方法。具体实现方法如下: 1. 在 QGraphicsItem鼠标事件处理函数中调用 QGraphicsView鼠标事件处理函数: ```cpp void MyGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { // 处理自己的鼠标事件 // 调用 QGraphicsView鼠标事件处理函数 QGraphicsView *view = scene()->views().first(); view->mousePressEvent(event); // 不要忘记调用父类的函数 QGraphicsItem::mousePressEvent(event); } ``` 2. 在 QGraphicsView鼠标事件处理函数中调用 callSuper() 方法: ```cpp void MyGraphicsView::mousePressEvent(QMouseEvent *event) { // 处理自己的鼠标事件 // 调用父类的鼠标事件处理函数 QGraphicsView::mousePressEvent(event); // 调用 QGraphicsItem鼠标事件处理函数 QGraphicsItem *item = itemAt(event->pos()); if (item != nullptr) { QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMousePress); mouseEvent.setPos(item->mapFromScene(mapToScene(event->pos()))); mouseEvent.setScenePos(mapToScene(event->pos())); mouseEvent.setScreenPos(event->globalPos()); mouseEvent.setButton(event->button()); mouseEvent.setButtons(event->buttons()); mouseEvent.setModifiers(event->modifiers()); item->mousePressEvent(&mouseEvent); } } ``` 注意,在调用 QGraphicsView鼠标事件处理函数之后,要调用父类的鼠标事件处理函数。另外,在调用 QGraphicsItem鼠标事件处理函数,要将事件转换为 QGraphicsSceneMouseEvent,并设置对应的属性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值