当前位置:   article > 正文

Qt第二十五章:QWidget隐藏标题栏后-缩放、移动、圆角窗口_qwidget 隐藏标题栏

qwidget 隐藏标题栏

 

  1. 隐藏标题栏
    self.setWindowFlags(Qt.FramelessWindowHint)
  2. 工具类(读者直接复制到项目中)
    1. class QWindowMoveResizeWidget(QWidget):
    2. def __init__(self, parent=None):
    3. super(QWindowMoveResizeWidget, self).__init__(parent)
    4. # 1.设置无边框 和 透明背景 无边框必须设置全,不然会导致点击任务栏不能最小化窗口
    5. self.setWindowFlags(
    6. Qt.Window
    7. | Qt.FramelessWindowHint
    8. | Qt.WindowSystemMenuHint
    9. | Qt.WindowMinimizeButtonHint
    10. | Qt.WindowMaximizeButtonHint
    11. )
    12. # 设置窗体透明
    13. self.setAttribute(Qt.WA_TranslucentBackground)
    14. # 默认标题栏高度 必须设
    15. self.DEFAULT_TITILE_BAR_HEIGHT = 40
    16. # 鼠标缩放窗口最小宽度,必须设
    17. self.MIN_WINDOW_WIDTH = 10
    18. self.MIN_WINDOW_HEIGHT = 10
    19. # 鼠标拖动窗口的标识
    20. self.m_flag = False
    21. # 初始化鼠标拖动标题栏标志
    22. self.drag_flag = False
    23. # 记录按下时窗口坐标, 这个用于窗口移动
    24. self.win_x = 0
    25. self.win_y = 0
    26. # 记录按下时鼠标坐标,这个用于计算鼠标移动的距离
    27. self.mouse_x = 0
    28. self.mouse_y = 0
    29. # 记录鼠标移入的拖动区域,共8种区域 左上 左 左下 上 下 右上 右 右下
    30. self.left_up = None
    31. self.left = None
    32. self.left_down = None
    33. self.up = None
    34. self.down = None
    35. self.right_up = None
    36. self.right = None
    37. self.right_down = None
    38. # 设置为True则mouseMoveEvent事件不需要按下也能触发,不然要按着鼠标左键或右键才能触发
    39. self.setMouseTracking(True)
    40. # 设置子类的mousetrack
    41. # self.centralwidget.setMouseTracking(True)
    42. # 记录按下时窗口的大小,用于计算鼠标相对于窗口移动的距离,用于缩放
    43. self.win_w = 0
    44. self.win_h = 0
    45. # 初始化鼠标缩放标志
    46. self.move_left_up_flag = False
    47. self.move_left_flag = False
    48. self.move_left_down_flag = False
    49. self.move_up_flag = False
    50. self.move_down_flag = False
    51. self.move_right_up_flag = False
    52. self.move_right_flag = False
    53. self.move_right_down_flag = False
    54. # 设置边框圆角
    55. def paintEvent(self, event: PySide6.QtGui.QPaintEvent) -> None:
    56. painter = QPainter(self)
    57. painter.setRenderHint(QPainter.RenderHint.Antialiasing) # 设置抗锯齿,不然边框会有明显锯齿
    58. painter.setBrush(Qt.white) # 设置窗体颜色
    59. painter.drawRoundedRect(self.rect(), 10, 10)
    60. super().paintEvent(event)
    61. def resizeEvent(self, a0: QtGui.QResizeEvent) -> None:
    62. """
    63. @description 窗口缩放事件
    64. @param
    65. @return
    66. """
    67. # 最大化最小化的时候,需要去改变按钮组位置
    68. # self.titleBar.close_btn.move(self.width() - 33, 10)
    69. # self.titleBar.max_btn.move(self.width() - 66, 10)
    70. # self.titleBar.min_btn.move(self.width() - 99, 10)
    71. # self.titleBar.title.resize(self.width(), DEFAULT_TITILE_BAR_HEIGHT)
    72. # 记录鼠标移入的拖动区域,共8种区域
    73. self.left_up = QRect(0, 0, 10, 10)
    74. self.left = QRect(0, 10, 10, self.height() - 20)
    75. self.left_down = QRect(0, self.height() - 10, 10, 10)
    76. self.up = QRect(10, 0, self.width() - 20, 10)
    77. self.down = QRect(10, self.height() - 10, self.width() - 20, 10)
    78. self.right_up = QRect(self.width() - 10, 0, 10, 10)
    79. self.right = QRect(self.width() - 10, 10, 10, self.height() - 20)
    80. self.right_down = QRect(self.width() - 10, self.height() - 10, 10, 10)
    81. return super().resizeEvent(a0)
    82. def mousePressEvent(self, a0: QtGui.QMouseEvent) -> None:
    83. """
    84. 拖动窗口
    85. """
    86. if a0.button() == QtCore.Qt.LeftButton and self.isMaximized() == False and self.cursor().shape() == QtGui.QCursor(
    87. QtCore.Qt.ArrowCursor).shape():
    88. self.m_flag = True
    89. self.m_Position = a0.globalPosition().toPoint() - self.pos() # 获取鼠标相对窗口的位置
    90. a0.accept()
    91. self.setCursor(QtGui.QCursor(QtCore.Qt.OpenHandCursor)) # 更改鼠标图标
    92. else:
    93. """
    94. @description 鼠标按下事件
    95. @param
    96. @return
    97. """
    98. # 记录按下时窗口坐标, 这个用于窗口移动
    99. self.win_x = self.x()
    100. self.win_y = self.y()
    101. # 记录按下时鼠标坐标,这个用于计算鼠标移动的距离
    102. self.mouse_x = a0.globalPosition().x()
    103. self.mouse_y = a0.globalPosition().y()
    104. # 记录按下时窗口的大小,用于计算鼠标相对于窗口移动的距离,用于缩放
    105. self.win_w = self.width()
    106. self.win_h = self.height()
    107. if not self.isMaximized():
    108. # 如果按下的是鼠标左键
    109. if a0.button() == Qt.MouseButton.LeftButton and self.left_up.contains(a0.position().x(),
    110. a0.position().y()):
    111. self.move_left_up_flag = True
    112. if a0.button() == Qt.MouseButton.LeftButton and self.left.contains(a0.position().x(),
    113. a0.position().y()):
    114. self.move_left_flag = True
    115. if a0.button() == Qt.MouseButton.LeftButton and self.left_down.contains(
    116. a0.position().x(), a0.position().y()
    117. ):
    118. self.move_left_down_flag = True
    119. if a0.button() == Qt.MouseButton.LeftButton and self.up.contains(a0.position().x(), a0.position().y()):
    120. self.move_up_flag = True
    121. if a0.button() == Qt.MouseButton.LeftButton and self.down.contains(a0.position().x(),
    122. a0.position().y()):
    123. self.move_down_flag = True
    124. if a0.button() == Qt.MouseButton.LeftButton and self.right_up.contains(
    125. a0.position().x(), a0.position().y()
    126. ):
    127. self.move_right_up_flag = True
    128. if a0.button() == Qt.MouseButton.LeftButton and self.right.contains(a0.position().x(),
    129. a0.position().y()):
    130. self.move_right_flag = True
    131. if a0.button() == Qt.MouseButton.LeftButton and self.right_down.contains(
    132. a0.position().x(), a0.position().y()
    133. ):
    134. self.move_right_down_flag = True
    135. return super().mousePressEvent(a0)
    136. def mouseMoveEvent(self, a0: QtGui.QMouseEvent) -> None:
    137. """
    138. 拖动窗口
    139. """
    140. if QtCore.Qt.LeftButton and self.m_flag and self.cursor().shape() == QtGui.QCursor(
    141. QtCore.Qt.OpenHandCursor).shape():
    142. self.move(a0.globalPosition().toPoint() - self.m_Position) # 更改窗口位置
    143. a0.accept()
    144. else:
    145. """
    146. @description 鼠标按下移动事件
    147. @param
    148. @return
    149. """
    150. # 获取移动后鼠标的位置
    151. mouse_move_x = a0.globalPosition().x()
    152. mouse_move_y = a0.globalPosition().y()
    153. # 计算移动的距离
    154. offset_x = mouse_move_x - self.mouse_x
    155. offset_y = mouse_move_y - self.mouse_y
    156. # 移动鼠标时设置鼠标样式
    157. if not self.isMaximized():
    158. # 不是拖动的时才可能是缩放状态
    159. if not self.drag_flag:
    160. # 左上
    161. if self.left_up.contains(a0.position().x(), a0.position().y()):
    162. self.setCursor(Qt.SizeFDiagCursor)
    163. # 左
    164. elif self.left.contains(a0.position().x(), a0.position().y()):
    165. self.setCursor(Qt.SizeHorCursor)
    166. # 左下
    167. elif self.left_down.contains(a0.position().x(), a0.position().y()):
    168. self.setCursor(Qt.SizeBDiagCursor)
    169. # 上
    170. elif self.up.contains(a0.position().x(), a0.position().y()):
    171. self.setCursor(Qt.SizeVerCursor)
    172. # 下
    173. elif self.down.contains(a0.position().x(), a0.position().y()):
    174. self.setCursor(Qt.SizeVerCursor)
    175. # 右上
    176. elif self.right_up.contains(a0.position().x(), a0.position().y()):
    177. self.setCursor(Qt.SizeBDiagCursor)
    178. # 右
    179. elif self.right.contains(a0.position().x(), a0.position().y()):
    180. self.setCursor(Qt.SizeHorCursor)
    181. # 右下
    182. elif self.right_down.contains(a0.position().x(), a0.position().y()):
    183. self.setCursor(Qt.SizeFDiagCursor)
    184. else:
    185. self.setCursor(Qt.ArrowCursor)
    186. else:
    187. self.setCursor(Qt.ArrowCursor)
    188. else:
    189. self.setCursor(Qt.ArrowCursor)
    190. # 如果按下且在左上角范围内则缩放(其他代码参考左上)
    191. if self.move_left_up_flag:
    192. # 拖动的时候也要设置一下形状
    193. self.setCursor(Qt.SizeFDiagCursor)
    194. resize_w = self.win_w - offset_x
    195. resize_h = self.win_h - offset_y
    196. # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
    197. resize_w = self.MIN_WINDOW_WIDTH if resize_w < self.MIN_WINDOW_WIDTH else resize_w
    198. resize_h = self.MIN_WINDOW_HEIGHT if resize_h < self.MIN_WINDOW_HEIGHT else resize_h
    199. # 设置窗口缩放尺寸
    200. self.resize(resize_w, resize_h)
    201. # 设置窗口移动,需要鼠标跟随
    202. # x y 都要鼠标跟随
    203. if resize_w != self.MIN_WINDOW_WIDTH and resize_h != self.MIN_WINDOW_HEIGHT:
    204. self.move(self.win_x + offset_x, self.win_y + offset_y)
    205. # 缩放宽度等于最小宽度,高度鼠标跟随
    206. if resize_w == self.MIN_WINDOW_WIDTH and resize_h != self.MIN_WINDOW_HEIGHT:
    207. self.move(self.x(), self.win_y + offset_y)
    208. # 缩放高度等于最小高度,宽度鼠标跟随
    209. if resize_w != self.MIN_WINDOW_WIDTH and resize_h == self.MIN_WINDOW_HEIGHT:
    210. self.move(self.win_x + offset_x, self.y())
    211. # 如果按下且在左边范围内则缩放
    212. elif self.move_left_flag:
    213. # 拖动的时候也要设置一下形状
    214. self.setCursor(Qt.SizeHorCursor)
    215. resize_w = self.win_w - offset_x
    216. resize_h = self.win_h
    217. # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
    218. resize_w = self.MIN_WINDOW_WIDTH if resize_w < self.MIN_WINDOW_WIDTH else resize_w
    219. # 设置窗口缩放尺寸
    220. self.resize(resize_w, resize_h)
    221. # 设置窗口移动,需要鼠标跟随
    222. # 只要宽度鼠标跟随
    223. if resize_w != self.MIN_WINDOW_WIDTH:
    224. self.move(self.win_x + offset_x, self.win_y)
    225. # 如果按下且在左下角范围内则缩放
    226. elif self.move_left_down_flag:
    227. # 拖动的时候也要设置一下形状
    228. self.setCursor(Qt.SizeBDiagCursor)
    229. resize_w = self.win_w - offset_x
    230. resize_h = self.win_h + offset_y
    231. # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
    232. resize_w = self.MIN_WINDOW_WIDTH if resize_w < self.MIN_WINDOW_WIDTH else resize_w
    233. resize_h = self.MIN_WINDOW_HEIGHT if resize_h < self.MIN_WINDOW_HEIGHT else resize_h
    234. # 设置窗口缩放尺寸
    235. self.resize(resize_w, resize_h)
    236. # 设置窗口移动,需要鼠标跟随
    237. # x y 都要鼠标跟随
    238. if resize_w != self.MIN_WINDOW_WIDTH and resize_h != self.MIN_WINDOW_HEIGHT:
    239. self.move(self.win_x + offset_x, self.y())
    240. # 缩放高度等于最小高度,宽度鼠标跟随
    241. if resize_w != self.MIN_WINDOW_WIDTH and resize_h == self.MIN_WINDOW_HEIGHT:
    242. self.move(self.win_x + offset_x, self.y())
    243. # 如果按下且在上边范围内则缩放
    244. elif self.move_up_flag:
    245. # 拖动的时候也要设置一下形状
    246. self.setCursor(Qt.SizeVerCursor)
    247. resize_w = self.win_w
    248. resize_h = self.win_h - offset_y
    249. # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
    250. resize_h = self.MIN_WINDOW_HEIGHT if resize_h < self.MIN_WINDOW_HEIGHT else resize_h
    251. # 设置窗口缩放尺寸
    252. self.resize(resize_w, resize_h)
    253. # 设置窗口移动,需要鼠标跟随
    254. # 只要高度鼠标跟随
    255. if resize_h != self.MIN_WINDOW_HEIGHT:
    256. self.move(self.win_x, self.win_y + offset_y)
    257. # 如果按下且在下边范围内则缩放
    258. elif self.move_down_flag:
    259. # 拖动的时候也要设置一下形状
    260. self.setCursor(Qt.SizeVerCursor)
    261. resize_w = self.win_w
    262. resize_h = self.win_h + offset_y
    263. # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
    264. resize_h = self.MIN_WINDOW_HEIGHT if resize_h < self.MIN_WINDOW_HEIGHT else resize_h
    265. # 设置窗口缩放尺寸
    266. self.resize(resize_w, resize_h)
    267. # 如果按下且在右上角范围内则缩放
    268. elif self.move_right_up_flag:
    269. # 拖动的时候也要设置一下形状
    270. self.setCursor(Qt.SizeBDiagCursor)
    271. resize_w = self.win_w + offset_x
    272. resize_h = self.win_h - offset_y
    273. # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
    274. resize_w = self.MIN_WINDOW_WIDTH if resize_w < self.MIN_WINDOW_WIDTH else resize_w
    275. resize_h = self.MIN_WINDOW_HEIGHT if resize_h < self.MIN_WINDOW_HEIGHT else resize_h
    276. # 设置窗口缩放尺寸
    277. self.resize(resize_w, resize_h)
    278. # 设置窗口移动,需要鼠标跟随
    279. # x y 都要鼠标跟随
    280. if resize_w != self.MIN_WINDOW_WIDTH and resize_h != self.MIN_WINDOW_HEIGHT:
    281. self.move(self.win_x, self.win_y + offset_y)
    282. # 缩放宽度等于最小宽度,高度鼠标跟随
    283. if resize_w == self.MIN_WINDOW_WIDTH and resize_h != self.MIN_WINDOW_HEIGHT:
    284. self.move(self.x(), self.win_y + offset_y)
    285. # 如果按下且在右边范围内则缩放
    286. elif self.move_right_flag:
    287. # 拖动的时候也要设置一下形状
    288. self.setCursor(Qt.SizeHorCursor)
    289. resize_w = self.win_w + offset_x
    290. resize_h = self.win_h
    291. # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
    292. resize_w = self.MIN_WINDOW_WIDTH if resize_w < self.MIN_WINDOW_WIDTH else resize_w
    293. # 设置窗口缩放尺寸
    294. self.resize(resize_w, resize_h)
    295. # 如果按下且在右下角范围内则缩放
    296. elif self.move_right_down_flag:
    297. # 拖动的时候也要设置一下形状
    298. self.setCursor(Qt.SizeFDiagCursor)
    299. resize_w = self.win_w + offset_x
    300. resize_h = self.win_h + offset_y
    301. # 如果缩放后的尺寸小于最小尺寸则窗口不能缩放了
    302. resize_w = self.MIN_WINDOW_WIDTH if resize_w < self.MIN_WINDOW_WIDTH else resize_w
    303. resize_h = self.MIN_WINDOW_HEIGHT if resize_h < self.MIN_WINDOW_HEIGHT else resize_h
    304. # 设置窗口缩放尺寸
    305. self.resize(resize_w, resize_h)
    306. # 如果按下才能移动
    307. elif self.drag_flag:
    308. # 设置窗口移动的距离
    309. self.move(self.win_x + offset_x, self.win_y + offset_y)
    310. return super().mouseMoveEvent(a0)
    311. def mouseReleaseEvent(self, a0: QtGui.QMouseEvent) -> None:
    312. self.m_flag = False
    313. self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
    314. """
    315. @description 鼠标按下松开事件
    316. @param
    317. @return
    318. """
    319. self.drag_flag = False
    320. self.move_left_up_flag = False
    321. self.move_left_flag = False
    322. self.move_left_down_flag = False
    323. self.move_up_flag = False
    324. self.move_down_flag = False
    325. self.move_right_up_flag = False
    326. self.move_right_flag = False
    327. self.move_right_down_flag = False
    328. self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
    329. return super().mouseReleaseEvent(a0)
  3. 使用 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/234902
推荐阅读
相关标签
  

闽ICP备14008679号