赞
踩
self.setWindowFlags(Qt.FramelessWindowHint)
- class QWindowMoveResizeWidget(QWidget):
-
- def __init__(self, parent=None):
- super(QWindowMoveResizeWidget, self).__init__(parent)
- # 1.设置无边框 和 透明背景 无边框必须设置全,不然会导致点击任务栏不能最小化窗口
- self.setWindowFlags(
- Qt.Window
- | Qt.FramelessWindowHint
- | Qt.WindowSystemMenuHint
- | Qt.WindowMinimizeButtonHint
- | Qt.WindowMaximizeButtonHint
- )
- # 设置窗体透明
- self.setAttribute(Qt.WA_TranslucentBackground)
- # 默认标题栏高度 必须设
- self.DEFAULT_TITILE_BAR_HEIGHT = 40
- # 鼠标缩放窗口最小宽度,必须设
- self.MIN_WINDOW_WIDTH = 10
- self.MIN_WINDOW_HEIGHT = 10
- # 鼠标拖动窗口的标识
- self.m_flag = False
- # 初始化鼠标拖动标题栏标志
- self.drag_flag = False
- # 记录按下时窗口坐标, 这个用于窗口移动
- self.win_x = 0
- self.win_y = 0
- # 记录按下时鼠标坐标,这个用于计算鼠标移动的距离
- self.mouse_x = 0
- self.mouse_y = 0
- # 记录鼠标移入的拖动区域,共8种区域 左上 左 左下 上 下 右上 右 右下
- self.left_up = None
- self.left = None
- self.left_down = None
- self.up = None
- self.down = None
- self.right_up = None
- self.right = None
- self.right_down = None
- # 设置为True则mouseMoveEvent事件不需要按下也能触发,不然要按着鼠标左键或右键才能触发
- self.setMouseTracking(True)
- # 设置子类的mousetrack
- # self.centralwidget.setMouseTracking(True)
- # 记录按下时窗口的大小,用于计算鼠标相对于窗口移动的距离,用于缩放
- self.win_w = 0
- self.win_h = 0
- # 初始化鼠标缩放标志
- self.move_left_up_flag = False
- self.move_left_flag = False
- self.move_left_down_flag = False
- self.move_up_flag = False
- self.move_down_flag = False
- self.move_right_up_flag = False
- self.move_right_flag = False
- self.move_right_down_flag = False
-
- # 设置边框圆角
- def paintEvent(self, event: PySide6.QtGui.QPaintEvent) -> None:
- painter = QPainter(self)
- painter.setRenderHint(QPainter.RenderHint.Antialiasing) # 设置抗锯齿,不然边框会有明显锯齿
- painter.setBrush(Qt.white) # 设置窗体颜色
- painter.drawRoundedRect(self.rect(), 10, 10)
- super().paintEvent(event)
-
- def resizeEvent(self, a0: QtGui.QResizeEvent) -> None:
- """
- @description 窗口缩放事件
- @param
- @return
- """
- # 最大化最小化的时候,需要去改变按钮组位置
- # self.titleBar.close_btn.move(self.width() - 33, 10)
- # self.titleBar.max_btn.move(self.width() - 66, 10)
- # self.titleBar.min_btn.move(self.width() - 99, 10)
- # self.titleBar.title.resize(self.width(), DEFAULT_TITILE_BAR_HEIGHT)
-
- # 记录鼠标移入的拖动区域,共8种区域
- self.left_up = QRect(0, 0, 10, 10)
- self.left = QRect(0, 10, 10, self.height() - 20)
- self.left_down = QRect(0, self.height() - 10, 10, 10)
- self.up = QRect(10, 0, self.width() - 20, 10)
- self.down = QRect(10, self.height() - 10, self.width() - 20, 10)
- self.right_up = QRect(self.width() - 10, 0, 10, 10)
- self.right = QRect(self.width() - 10, 10, 10, self.height() - 20)
- self.right_down = QRect(self.width() - 10, self.height() - 10, 10, 10)
-
- return super().resizeEvent(a0)
-
- def mousePressEvent(self, a0: QtGui.QMouseEvent) -> None:
- """
- 拖动窗口
- """
- if a0.button() == QtCore.Qt.LeftButton and self.isMaximized() == False and self.cursor().shape() == QtGui.QCursor(
- QtCore.Qt.ArrowCursor).shape():
- self.m_flag = True
- self.m_Position = a0.globalPosition().toPoint() - self.pos() # 获取鼠标相对窗口的位置
- a0.accept()
- self.setCursor(QtGui.QCursor(QtCore.Qt.OpenHandCursor)) # 更改鼠标图标
- else:
- """
- @description 鼠标按下事件
- @param
- @return
- """
- # 记录按下时窗口坐标, 这个用于窗口移动
- self.win_x = self.x()
- self.win_y = self.y()
- # 记录按下时鼠标坐标,这个用于计算鼠标移动的距离
- self.mouse_x = a0.globalPosition().x()
- self.mouse_y = a0.globalPosition().y()
- # 记录按下时窗口的大小,用于计算鼠标相对于窗口移动的距离,用于缩放
- self.win_w = self.width()
- self.win_h = self.height()
-
- if not self.isMaximized():
- # 如果按下的是鼠标左键
- if a0.button() == Qt.MouseButton.LeftButton and self.left_up.contains(a0.position().x(),
- a0.position().y()):
- self.move_left_up_flag = True
- if a0.button() == Qt.MouseButton.LeftButton and self.left.contains(a0.position().x(),
- a0.position().y()):
- self.move_left_flag = True
- if a0.button() == Qt.MouseButton.LeftButton and self.left_down.contains(
- a0.position().x(), a0.position().y()
- ):
- self.move_left_down_flag = True
- if a0.button() == Qt.MouseButton.LeftButton and self.up.contains(a0.position().x(), a0.position().y()):
- self.move_up_flag = True
- if a0.button() == Qt.MouseButton.LeftButton and self.down.contains(a0.position().x(),
- a0.position().y()):
- self.move_down_flag = True
- if a0.button() == Qt.MouseButton.LeftButton and self.right_up.contains(
- a0.position().x(), a0.position().y()
- ):
- self.move_right_up_flag = True
- if a0.button() == Qt.MouseButton.LeftButton and self.right.contains(a0.position().x(),
- a0.position().y()):
- self.move_right_flag = True
- if a0.button() == Qt.MouseButton.LeftButton and self.right_down.contains(
- a0.position().x(), a0.position().y()
- ):
- self.move_right_down_flag = True
- return super().mousePressEvent(a0)
-
- def mouseMoveEvent(self, a0: QtGui.QMouseEvent) -> None:
- """
- 拖动窗口
- """
- if QtCore.Qt.LeftButton and self.m_flag and self.cursor().shape() == QtGui.QCursor(
- QtCore.Qt.OpenHandCursor).shape():
- self.move(a0.globalPosition().toPoint() - self.m_Position) # 更改窗口位置
- a0.accept()
- else:
- """
- @description 鼠标按下移动事件
- @param
- @return
- """
- # 获取移动后鼠标的位置
- mouse_move_x = a0.globalPosition().x()
- mouse_move_y = a0.globalPosition().y()
- # 计算移动的距离
- offset_x = mouse_move_x - self.mouse_x
- offset_y = mouse_move_y - self.mouse_y
- # 移动鼠标时设置鼠标样式
- if not self.isMaximized():
- # 不是拖动的时才可能是缩放状态
- if not self.drag_flag:
- # 左上
- if self.left_up.contains(a0.position().x(), a0.position().y()):
- self.setCursor(Qt.SizeFDiagCursor)
- # 左
- elif self.left.contains(a0.position().x(), a0.position().y()):
- self.setCursor(Qt.SizeHorCursor)
- # 左下
- elif self.left_down.contains(a0.position().x(), a0.position().y()):
- self.setCursor(Qt.SizeBDiagCursor)
- # 上
- elif self.up.contains(a0.position().x(), a0.position().y()):
- self.setCursor(Qt.SizeVerCursor)
- # 下
- elif self.down.contains(a0.position().x(), a0.position().y()):
- self.setCursor(Qt.SizeVerCursor)
- # 右上
- elif self.right_up.contains(a0.position().x(), a0.position().y()):
- self.setCursor(Qt.SizeBDiagCursor)
- # 右
- elif self.right.contains(a0.position().x(), a0.position().y()):
- self.setCursor(Qt.SizeHorCursor)
- # 右下
- elif self.right_down.contains(a0.position().x(), a0.position().y()):
- self.setCursor(Qt.SizeFDiagCursor)
- else:
- self.setCursor(Qt.ArrowCursor)
- else:
- self.setCursor(Qt.ArrowCursor)
- else:
- self.setCursor(Qt.ArrowCursor)
- # 如果按下且在左上角范围内则缩放(其他代码参考左上)
- if self.move_left_up_flag:
- # 拖动的时候也要设置一下形状
- self.setCursor(Qt.SizeFDiagCursor)
- resize_w = self.win_w - offset_x
- resize_h = self.win_h - offset_y
- # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
- resize_w = self.MIN_WINDOW_WIDTH if resize_w < self.MIN_WINDOW_WIDTH else resize_w
- resize_h = self.MIN_WINDOW_HEIGHT if resize_h < self.MIN_WINDOW_HEIGHT else resize_h
- # 设置窗口缩放尺寸
- self.resize(resize_w, resize_h)
- # 设置窗口移动,需要鼠标跟随
- # x y 都要鼠标跟随
- if resize_w != self.MIN_WINDOW_WIDTH and resize_h != self.MIN_WINDOW_HEIGHT:
- self.move(self.win_x + offset_x, self.win_y + offset_y)
- # 缩放宽度等于最小宽度,高度鼠标跟随
- if resize_w == self.MIN_WINDOW_WIDTH and resize_h != self.MIN_WINDOW_HEIGHT:
- self.move(self.x(), self.win_y + offset_y)
- # 缩放高度等于最小高度,宽度鼠标跟随
- if resize_w != self.MIN_WINDOW_WIDTH and resize_h == self.MIN_WINDOW_HEIGHT:
- self.move(self.win_x + offset_x, self.y())
- # 如果按下且在左边范围内则缩放
- elif self.move_left_flag:
- # 拖动的时候也要设置一下形状
- self.setCursor(Qt.SizeHorCursor)
- resize_w = self.win_w - offset_x
- resize_h = self.win_h
- # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
- resize_w = self.MIN_WINDOW_WIDTH if resize_w < self.MIN_WINDOW_WIDTH else resize_w
- # 设置窗口缩放尺寸
- self.resize(resize_w, resize_h)
- # 设置窗口移动,需要鼠标跟随
- # 只要宽度鼠标跟随
- if resize_w != self.MIN_WINDOW_WIDTH:
- self.move(self.win_x + offset_x, self.win_y)
- # 如果按下且在左下角范围内则缩放
- elif self.move_left_down_flag:
- # 拖动的时候也要设置一下形状
- self.setCursor(Qt.SizeBDiagCursor)
- resize_w = self.win_w - offset_x
- resize_h = self.win_h + offset_y
- # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
- resize_w = self.MIN_WINDOW_WIDTH if resize_w < self.MIN_WINDOW_WIDTH else resize_w
- resize_h = self.MIN_WINDOW_HEIGHT if resize_h < self.MIN_WINDOW_HEIGHT else resize_h
- # 设置窗口缩放尺寸
- self.resize(resize_w, resize_h)
- # 设置窗口移动,需要鼠标跟随
- # x y 都要鼠标跟随
- if resize_w != self.MIN_WINDOW_WIDTH and resize_h != self.MIN_WINDOW_HEIGHT:
- self.move(self.win_x + offset_x, self.y())
- # 缩放高度等于最小高度,宽度鼠标跟随
- if resize_w != self.MIN_WINDOW_WIDTH and resize_h == self.MIN_WINDOW_HEIGHT:
- self.move(self.win_x + offset_x, self.y())
- # 如果按下且在上边范围内则缩放
- elif self.move_up_flag:
- # 拖动的时候也要设置一下形状
- self.setCursor(Qt.SizeVerCursor)
- resize_w = self.win_w
- resize_h = self.win_h - offset_y
- # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
- resize_h = self.MIN_WINDOW_HEIGHT if resize_h < self.MIN_WINDOW_HEIGHT else resize_h
- # 设置窗口缩放尺寸
- self.resize(resize_w, resize_h)
- # 设置窗口移动,需要鼠标跟随
- # 只要高度鼠标跟随
- if resize_h != self.MIN_WINDOW_HEIGHT:
- self.move(self.win_x, self.win_y + offset_y)
- # 如果按下且在下边范围内则缩放
- elif self.move_down_flag:
- # 拖动的时候也要设置一下形状
- self.setCursor(Qt.SizeVerCursor)
- resize_w = self.win_w
- resize_h = self.win_h + offset_y
- # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
- resize_h = self.MIN_WINDOW_HEIGHT if resize_h < self.MIN_WINDOW_HEIGHT else resize_h
- # 设置窗口缩放尺寸
- self.resize(resize_w, resize_h)
- # 如果按下且在右上角范围内则缩放
- elif self.move_right_up_flag:
- # 拖动的时候也要设置一下形状
- self.setCursor(Qt.SizeBDiagCursor)
- resize_w = self.win_w + offset_x
- resize_h = self.win_h - offset_y
- # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
- resize_w = self.MIN_WINDOW_WIDTH if resize_w < self.MIN_WINDOW_WIDTH else resize_w
- resize_h = self.MIN_WINDOW_HEIGHT if resize_h < self.MIN_WINDOW_HEIGHT else resize_h
- # 设置窗口缩放尺寸
- self.resize(resize_w, resize_h)
- # 设置窗口移动,需要鼠标跟随
- # x y 都要鼠标跟随
- if resize_w != self.MIN_WINDOW_WIDTH and resize_h != self.MIN_WINDOW_HEIGHT:
- self.move(self.win_x, self.win_y + offset_y)
- # 缩放宽度等于最小宽度,高度鼠标跟随
- if resize_w == self.MIN_WINDOW_WIDTH and resize_h != self.MIN_WINDOW_HEIGHT:
- self.move(self.x(), self.win_y + offset_y)
- # 如果按下且在右边范围内则缩放
- elif self.move_right_flag:
- # 拖动的时候也要设置一下形状
- self.setCursor(Qt.SizeHorCursor)
- resize_w = self.win_w + offset_x
- resize_h = self.win_h
- # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
- resize_w = self.MIN_WINDOW_WIDTH if resize_w < self.MIN_WINDOW_WIDTH else resize_w
- # 设置窗口缩放尺寸
- self.resize(resize_w, resize_h)
- # 如果按下且在右下角范围内则缩放
- elif self.move_right_down_flag:
- # 拖动的时候也要设置一下形状
- self.setCursor(Qt.SizeFDiagCursor)
- resize_w = self.win_w + offset_x
- resize_h = self.win_h + offset_y
- # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
- resize_w = self.MIN_WINDOW_WIDTH if resize_w < self.MIN_WINDOW_WIDTH else resize_w
- resize_h = self.MIN_WINDOW_HEIGHT if resize_h < self.MIN_WINDOW_HEIGHT else resize_h
- # 设置窗口缩放尺寸
- self.resize(resize_w, resize_h)
- # 如果按下才能移动
- elif self.drag_flag:
- # 设置窗口移动的距离
- self.move(self.win_x + offset_x, self.win_y + offset_y)
- return super().mouseMoveEvent(a0)
-
- def mouseReleaseEvent(self, a0: QtGui.QMouseEvent) -> None:
- self.m_flag = False
- self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
- """
- @description 鼠标按下松开事件
- @param
- @return
- """
- self.drag_flag = False
- self.move_left_up_flag = False
- self.move_left_flag = False
- self.move_left_down_flag = False
- self.move_up_flag = False
- self.move_down_flag = False
- self.move_right_up_flag = False
- self.move_right_flag = False
- self.move_right_down_flag = False
- self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
- return super().mouseReleaseEvent(a0)

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。