QWidget::mousePressEvent(event)加多一行也是可以的。
有的重载函数要调用基类的实现,有的不用。这到底从哪判定的?
如果是MFC,一般它会自动生成代码,把架构搭好了。现在Qt简洁了,自行添加。
- C/C++ code
void editPainter::mousePressEvent(QMouseEvent *event){ Q_UNUSED(event); m_bMousePressed = true;}void editPainter::mouseMoveEvent(QMouseEvent *event){ QPoint pt = event->pos(); if (!m_bDisChangeDir) { //根据鼠标位置,调整方向(鼠标释放状态下,此可不断变化) if (m_rcTopLeft.contains(pt)) { setCursor(Qt::SizeFDiagCursor); m_enAction = DRAG_TOP_LEFT; } else if (m_rcTopRight.contains(pt)) { setCursor(Qt::SizeBDiagCursor); m_enAction = DRAG_TOP_RIGHT; } else if (m_rcBottomLeft.contains(pt)) { setCursor(Qt::SizeBDiagCursor); m_enAction = DRAG_BOTTOM_LEFT; } else if (m_rcBottomRight.contains(pt)) { setCursor(Qt::SizeFDiagCursor); m_enAction = DRAG_BOTTOM_RIGHT; } else if (m_rcLeftMiddle.contains(pt)) { setCursor(Qt::SizeHorCursor); m_enAction = DRAG_LEFTMIDDLE; } else if (m_rcRightMiddle.contains(pt)) { setCursor(Qt::SizeHorCursor); m_enAction = DRAG_RIGHTMIDDLE; } else if (m_rcTopMiddle.contains(pt)) { setCursor(Qt::SizeVerCursor); m_enAction = DRAG_TOPMIDDLE; } else if (m_rcBottomMiddle.contains(pt)) { setCursor(Qt::SizeVerCursor); m_enAction = DRAG_BOTTOMMIDDLE; } else if (m_rectOfRedLine.contains(pt)) { setCursor(Qt::SizeAllCursor); m_enAction = DRAG_CENTER; } else { setCursor(Qt::ArrowCursor); m_enAction = NO_ACTION; } //鼠标按下后,方向确认,且不更改了 if (m_bMousePressed) { m_bDisChangeDir = true; } } calculatePaintRect(event, m_enAction); m_ptLast = event->pos();}void editPainter::mouseReleaseEvent(QMouseEvent *event){ Q_UNUSED(event); m_bMousePressed = false; m_bDisChangeDir = false;}
------解决方案--------------------
除非想截止信号传播,最好是调用基类的事件处理函数,否则会有意想不到的问题出现。