简单的窗口贴边隐藏
This commit is contained in:
parent
7659893912
commit
5fa1e8b9c2
3 changed files with 110 additions and 0 deletions
|
@ -47,6 +47,7 @@ encoding//\u754C\u9762\u7F8E\u5316/QSS\u7F8E\u5316\u4F8B\u5B50/QPushButton\u6309
|
|||
encoding//\u754C\u9762\u7F8E\u5316/QScrollBar\u6EDA\u52A8\u6761\u6837\u5F0F/ScrollBar.py=utf-8
|
||||
encoding//\u7A0B\u5E8F\u91CD\u542F/AutoRestart.py=utf-8
|
||||
encoding//\u7A97\u53E3\u91CD\u542F/RestartMainWindow.py=utf-8
|
||||
encoding//\u7B80\u5355\u7684\u7A97\u53E3\u8D34\u8FB9\u9690\u85CF/WeltHideWindow.py=utf-8
|
||||
encoding//\u817E\u8BAF\u89C6\u9891\u70ED\u64AD\u5217\u8868/TencentMovieHotPlay.py=utf-8
|
||||
encoding//\u817E\u8BAF\u89C6\u9891\u70ED\u64AD\u5217\u8868/TencentMovieHotPlay_Flow.py=utf-8
|
||||
encoding//\u81EA\u52A8\u66F4\u65B0/mylibs/testlibs.py=utf-8
|
||||
|
|
14
简单的窗口贴边隐藏/README.md
Normal file
14
简单的窗口贴边隐藏/README.md
Normal file
|
@ -0,0 +1,14 @@
|
|||
# 简单的窗口贴边隐藏
|
||||
|
||||
## 大概思路
|
||||
- 1.思路是当窗口进入左边,顶部,右边一半时,此时判断窗口的坐标
|
||||
- 2.如果窗口的x坐标小于0 则需要隐藏到左边
|
||||
- 3.如果窗口的y坐标小于0 则需要隐藏到顶部
|
||||
- 4.如果窗口的x坐标大于屏幕宽度-窗口宽度/2 则需要隐藏到右边
|
||||
|
||||
## 事件
|
||||
- 1.mousePressEvent,鼠标按下事件,主要记录按下的坐标
|
||||
- 1.mouseMoveEvent,鼠标移动事件,用于移动窗口
|
||||
- 1.mouseReleaseEvent,鼠标弹起事件,用于判断是否需要隐藏窗口
|
||||
- 1.enterEvent,鼠标进入事件,用于窗口隐藏后,是否需要暂时显示预览
|
||||
- 1.leaveEvent,鼠标离开事件,用于窗口暂时显示后自动隐藏效果
|
95
简单的窗口贴边隐藏/WeltHideWindow.py
Normal file
95
简单的窗口贴边隐藏/WeltHideWindow.py
Normal file
|
@ -0,0 +1,95 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Created on 2018年3月1日
|
||||
@author: Irony
|
||||
@site: https://github.com/892768447
|
||||
@email: 892768447@qq.com
|
||||
@file: WeltHideWindow
|
||||
@description:
|
||||
"""
|
||||
from PyQt5.QtCore import Qt
|
||||
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton
|
||||
|
||||
|
||||
__Author__ = 'By: Irony\nQQ: 892768447\nEmail: 892768447@qq.com'
|
||||
__Copyright__ = 'Copyright (c) 2018 Irony'
|
||||
__Version__ = 1.0
|
||||
|
||||
|
||||
class WeltHideWindow(QWidget):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(WeltHideWindow, self).__init__(*args, **kwargs)
|
||||
self.setWindowFlag(Qt.FramelessWindowHint, True)
|
||||
self.resize(800, 600)
|
||||
self._width = QApplication.desktop().availableGeometry(self).width()
|
||||
layout = QVBoxLayout(self)
|
||||
layout.addWidget(QPushButton("关闭窗口", self, clicked=self.close))
|
||||
|
||||
def mousePressEvent(self, event):
|
||||
'''鼠标按下事件,需要记录下坐标self._pos 和 是否可移动self._canMove'''
|
||||
super(WeltHideWindow, self).mousePressEvent(event)
|
||||
if event.button() == Qt.LeftButton:
|
||||
self._pos = event.globalPos() - self.pos()
|
||||
# 当窗口最大化或者全屏时不可移动
|
||||
self._canMove = not self.isMaximized() or not self.isFullScreen()
|
||||
|
||||
def mouseMoveEvent(self, event):
|
||||
'''鼠标移动事件,动态调整窗口位置'''
|
||||
super(WeltHideWindow, self).mouseMoveEvent(event)
|
||||
if event.buttons() == Qt.LeftButton and self._canMove:
|
||||
self.move(event.globalPos() - self._pos)
|
||||
|
||||
def mouseReleaseEvent(self, event):
|
||||
'''鼠标弹起事件,这个时候需要判断窗口的左边是否符合贴到左边,顶部,右边一半'''
|
||||
super(WeltHideWindow, self).mouseReleaseEvent(event)
|
||||
self._canMove = False
|
||||
pos = self.pos()
|
||||
x = pos.x()
|
||||
y = pos.y()
|
||||
if x < 0:
|
||||
# 隐藏到左边
|
||||
return self.move(1 - self.width(), y)
|
||||
if y < 0:
|
||||
# 隐藏到顶部
|
||||
return self.move(x, 1 - self.height())
|
||||
if x > self._width - self.width() / 2: # 窗口进入右边一半距离
|
||||
# 隐藏到右边
|
||||
return self.move(self._width - 1, y)
|
||||
|
||||
def enterEvent(self, event):
|
||||
'''鼠标进入窗口事件,用于弹出显示窗口'''
|
||||
super(WeltHideWindow, self).enterEvent(event)
|
||||
pos = self.pos()
|
||||
x = pos.x()
|
||||
y = pos.y()
|
||||
if x < 0:
|
||||
return self.move(0, y)
|
||||
if y < 0:
|
||||
return self.move(x, 0)
|
||||
if x > self._width - self.width() / 2:
|
||||
return self.move(self._width - self.width(), y)
|
||||
|
||||
def leaveEvent(self, event):
|
||||
'''鼠标离开事件,如果原先窗口已经隐藏,并暂时显示,此时离开后需要再次隐藏'''
|
||||
super(WeltHideWindow, self).leaveEvent(event)
|
||||
pos = self.pos()
|
||||
x = pos.x()
|
||||
y = pos.y()
|
||||
if x == 0:
|
||||
return self.move(1 - self.width(), y)
|
||||
if y == 0:
|
||||
return self.move(x, 1 - self.height())
|
||||
if x == self._width - self.width():
|
||||
return self.move(self._width - 1, y)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
app = QApplication(sys.argv)
|
||||
w = WeltHideWindow()
|
||||
w.show()
|
||||
sys.exit(app.exec_())
|
Loading…
Reference in a new issue