Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

移动窗口会发生严重的跳动和抖动。修改为如下代码可修复 #1

Open
BeGild opened this issue Jul 21, 2023 · 0 comments

Comments

@BeGild
Copy link

BeGild commented Jul 21, 2023

class Monitor(QWidget):

def __init__(self):
    super().__init__()
   self._startPos = QPoint(0, 0)  # 记录鼠标按下时的窗口坐标
    self._wmGap = QPoint(0, 0)  # 记录窗口移动的距离
    self._timer = QTimer(self)
    self._timer.timeout.connect(self.moveWindow)

def mouseMoveEvent(self, event: QMouseEvent):
    # event.pos()减去最初相对窗口位置,获得移动距离(x,y)
    self._wmGap = event.pos() - self._startPos
    # 启动定时器,延迟处理窗口移动事件 防止抖动
    if not self._timer.isActive():
        self._timer.start(10)
def moveWindow(self):
    # 移动窗口,保持鼠标与窗口的相对位置不变
    # 检查是否移除了当前主屏幕
    # 左方界限
    final_pos = self.pos() + self._wmGap
    if self.frameGeometry().topLeft().x() + self._wmGap.x() <= 0:
        final_pos.setX(0)
    # 右方界限
    elif self.frameGeometry().bottomRight().x() + self._wmGap.x() >= self.screen_width:
        final_pos.setX(self.screen_width - self.window_width)
    # 上方界限
    if self.frameGeometry().topLeft().y() + self._wmGap.y() <= 0:
        final_pos.setY(0)
    # 下方界限
    elif self.frameGeometry().bottomRight().y() + self._wmGap.y() >= self.screen_height:
        final_pos.setY(self.screen_height - self.window_height)
    self.move(final_pos)
    # 停止定时器
    self._timer.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant