当前位置: 代码迷 >> QT开发 >> 视图框架下Item的鼠标移动事件,该怎么处理
  详细解决方案

视图框架下Item的鼠标移动事件,该怎么处理

热度:249   发布时间:2016-04-25 03:03:06.0
视图框架下Item的鼠标移动事件
本帖最后由 qq_16982701 于 2015-01-15 13:45:37 编辑
自定义item内绘图,要求鼠标移动时调用鼠标移动函数,设置setMouseTracking(true)
但鼠标在item内移动仍然没有响应,扔需要鼠标按下才有反应,为什么?
#include "rectitem.h"
#include<QtGui>

RectItem::RectItem()
{
    setFlags(ItemIsFocusable|ItemIsMovable);
    rect.setRect(0,0,100,100);
    this->setMouseTracking(true);
}

QRectF RectItem::boundingRect() const
{
    qreal penWidth=4;
    return QRectF(rect.adjusted(-penWidth,-penWidth,penWidth,penWidth));

}

void RectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
//    QBrush brush(Qt::red);
//    painter->setBrush(brush);
    painter->drawRect(rect);
}


void RectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if(event->pos().y()==rect.bottom())
    {
        qDebug()<<"bottom";
    }
    if(event->pos().y()==rect.top())
    {
        qDebug()<<"top";
    }

}

------解决思路----------------------
如果你期望mouseMoveEvent被调用是不行的 这个必须被点击后才可以
你如果期望不点就能触发  是要重载hoverMoveEvent
见Qt文档
This event handler, for event event, can be reimplemented to receive mouse move events for this item. If you do receive this event, you can be certain that this item also received a mouse press event, and that this item is the current mouse grabber.
  相关解决方案