fix: magnetic widget adsorb bug

This commit is contained in:
Barry 2021-04-17 13:56:44 +08:00
commit a4daf33bfe
2 changed files with 11 additions and 3 deletions

View file

@ -9,6 +9,7 @@ MagneticWidget::MagneticWidget(QWidget *adsorbWidget, AdsorbPositions adsorbPos)
Q_ASSERT(m_adsorbWidget); Q_ASSERT(m_adsorbWidget);
setParent(m_adsorbWidget); setParent(m_adsorbWidget);
setWindowFlags(windowFlags() | Qt::Tool); setWindowFlags(windowFlags() | Qt::Tool);
m_adsorbWidgetSize = m_adsorbWidget->size();
m_adsorbWidget->installEventFilter(this); m_adsorbWidget->installEventFilter(this);
} }
@ -30,6 +31,10 @@ bool MagneticWidget::eventFilter(QObject *watched, QEvent *event)
if (watched != m_adsorbWidget || !event) { if (watched != m_adsorbWidget || !event) {
return false; return false;
} }
// 始终记录adsorbWidget最新size
if (QEvent::Resize == event->type()) {
m_adsorbWidgetSize = m_adsorbWidget->size();
}
if (m_adsorbed && QEvent::Move == event->type()) { if (m_adsorbed && QEvent::Move == event->type()) {
move(m_adsorbWidget->pos() - m_relativePos); move(m_adsorbWidget->pos() - m_relativePos);
} }
@ -48,7 +53,7 @@ bool MagneticWidget::eventFilter(QObject *watched, QEvent *event)
pos.setY(pos.y() - m_relativePos.y()); pos.setY(pos.y() - m_relativePos.y());
break; break;
case AP_OUTSIDE_RIGHT: case AP_OUTSIDE_RIGHT:
pos.setX(pos.x() + m_adsorbWidget->width()); pos.setX(pos.x() + m_adsorbWidgetSize.width());
pos.setY(pos.y() - m_relativePos.y()); pos.setY(pos.y() - m_relativePos.y());
break; break;
case AP_OUTSIDE_TOP: case AP_OUTSIDE_TOP:
@ -165,8 +170,8 @@ void MagneticWidget::moveEvent(QMoveEvent *event)
void MagneticWidget::getGeometry(QRect &relativeWidgetRect, QRect &targetWidgetRect) void MagneticWidget::getGeometry(QRect &relativeWidgetRect, QRect &targetWidgetRect)
{ {
relativeWidgetRect.setTopLeft(m_adsorbWidget->pos()); relativeWidgetRect.setTopLeft(m_adsorbWidget->pos());
relativeWidgetRect.setWidth(m_adsorbWidget->width()); relativeWidgetRect.setWidth(m_adsorbWidgetSize.width());
relativeWidgetRect.setHeight(m_adsorbWidget->height()); relativeWidgetRect.setHeight(m_adsorbWidgetSize.height());
targetWidgetRect.setTopLeft(pos()); targetWidgetRect.setTopLeft(pos());
targetWidgetRect.setWidth(width()); targetWidgetRect.setWidth(width());

View file

@ -46,6 +46,9 @@ private:
QPoint m_relativePos; QPoint m_relativePos;
bool m_adsorbed = false; bool m_adsorbed = false;
QPointer<QWidget> m_adsorbWidget; QPointer<QWidget> m_adsorbWidget;
// 单独记录adsorbWidgetSize因为Widget setGeometry的时候会先收到Move事件后收到Resize事件
// 但是收到Move事件时Widget的size()已经是setGeometry指定的size了
QSize m_adsorbWidgetSize;
AdsorbPosition m_curAdsorbPosition; AdsorbPosition m_curAdsorbPosition;
}; };