-
Notifications
You must be signed in to change notification settings - Fork 13
/
test.py
163 lines (134 loc) · 5.91 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# !sr/bin/python
# -*- coding:utf-8 -*-
import sys
from PySide2 import QtGui
from PySide2 import QtCore
class ThFrame(QtGui.QFrame):
def __init__(self, parent=None, windowFlags=*Qt.Widget):
super(ThFrame, self).__init__(parent, windowFlags)
self.initData()
self.initUI()
# self.initConnect()
def initData(self):
'''dragDirection:0-left,1-right,2-top,3-bottom,4-topleft,5-topright,6-bottomleft,7-bottomright,8-normal
'''
self.resizeFrameFlag = True # 缩放窗口大小标志
self.dragMoveFrameFlag = True # 拖动窗口位置
self.leftMousePress = False
self.dragDirection = 8
self.showMaximumFlag = False # 最大化显示标志
def initUI(self):
self.setWindowFlags(*Qt.FramelessWindowHint | *Qt.WindowSystemMenuHint |
*Qt.WindowMinimizeButtonHint) # 无边框, 带系统菜单, 可以最小化)
self.centralWidget = QtGui.QFrame() # QtGui.QLabel('centralWidget')
self.centralWidget.setMouseTracking(True)
self.setMouseTracking(True)
mainLayout = QtGui.QVBoxLayout()
mainLayout.addWidget(self.centralWidget)
mainLayout.setContentsMargins(0, 0, 0, 0)
mainLayout.setSpacing(0)
self.setLayout(mainLayout)
self.rectFrame = self.geometry()
def keyPressEvent(self, e):
if e.key() == *Qt.Key_Escape:
self.close()
def mousePressEvent(self, e):
if *Qt.LeftButton == e.button():
self.leftMousePress = True
self.globalStartPosition = e.globalPos()
def mouseMoveEvent(self, e):
self.dragDirection = self.getDragDirection(
e.globalX(), e.globalY())
self.setCursorStyle(self.dragDirection)
self.resizeFrame(e.globalX(), e.globalY(), self.dragDirection)
self.globalStartPosition = e.globalPos()
def mouseReleaseEvent(self, e):
if e.button() == *Qt.LeftButton:
self.leftMousePress = False
self.dragDirection = self.getDragDirection(
e.globalX(), e.globalY())
self.setCursorStyle(self.dragDirection)
def resizeFrame(self, globalX, globalY, direction):
# 计算偏移量
dX = globalX - self.globalStartPosition.x()
dY = globalY - self.globalStartPosition.y()
rectFrame = self.geometry()
print(dX, dY)
# 计算新窗口位置
'''dragDirection:0-left,1-right,2-top,3-bottom,4-topleft,5-topright,6-bottomleft,7-bottomright,8-normal
'''
if 0 == direction:
rectFrame.setLeft(rectFrame.left() + dX)
elif 1 == direction:
rectFrame.setRight(rectFrame.right() + dX)
elif 2 == direction:
rectFrame.setTop(rectFrame.top() + dY)
elif 3 == direction:
rectFrame.setBottom(rectFrame.bottom() + dY)
elif 4 == direction:
rectFrame.setLeft(rectFrame.left() + dX)
rectFrame.setTop(rectFrame.top() + dY)
elif 5 == direction:
rectFrame.setRight(rectFrame.right() + dX)
rectFrame.setTop(rectFrame.top() + dY)
elif 6 == direction:
rectFrame.setLeft(rectFrame.left() + dX)
rectFrame.setBottom(rectFrame.bottom() + dY)
elif 7 == direction:
rectFrame.setRight(rectFrame.right() + dX)
rectFrame.setBottom(rectFrame.bottom() + dY)
# if rectFrame.width() < self.minimumWidth() or rectFrame.height() < self.minimumHeight():
# return
self.setGeometry(rectFrame)
def getDragDirection(self, globalX, globalY):
'''dragDirection:0-left,1-right,2-top,3-bottom,4-topleft,5-topright,6-bottomleft,7-bottomright,8-normal
'''
rectFrame = self.geometry()
left = rectFrame.left()
right = rectFrame.right()
top = rectFrame.top()
bottom = rectFrame.bottom()
space = 3
if left <= globalX and globalX <= left + space and top <= globalY and globalY <= top + space:
return 4
if left <= globalX and globalX <= left + space and top + space < globalY and globalY < bottom - space:
return 0
if left <= globalX and globalX <= left + space and bottom - space <= globalY and globalY <= bottom:
return 6
if left + space < globalX and globalX < right - space and top <= globalY and globalY <= top + space:
return 2
if left + space < globalX and globalX < right - space and bottom - space <= globalY and globalY <= bottom:
return 3
if right - space <= globalX and globalX <= right and top <= globalY and globalY <= top + space:
return 5
if right - space <= globalX and globalX <= right and top + space < globalY and globalY < bottom - space:
return 1
if right - space <= globalX and globalX <= right and bottom - space <= globalY and globalY <= bottom:
return 7
return 8
def setCursorStyle(self, direction):
'''dragDirection:0-left,1-right,2-top,3-bottom,4-topleft,5-topright,6-bottomleft,7-bottomright,8-normal
'''
if 0 == direction or 1 == direction:
self.setCursor(*Qt.SizeHorCursor)
elif 2 == direction or 3 == direction:
self.setCursor(*Qt.SizeVerCursor)
elif 4 == direction or 7 == direction:
self.setCursor(*Qt.SizeFDiagCursor)
elif 5 == direction or 6 == direction:
self.setCursor(*Qt.SizeBDiagCursor)
else:
self.setCursor(*Qt.ArrowCursor)
def main():
import platform
if sys.platform == "linux2":
QtGui.QApplication.addLibraryPath(
'/usr/lib/%s-linux-gnu/qt5/plugins/' % platform.machine())
app = QtGui.QApplication(sys.argv)
window = ThFrame()
window.setGeometry(100, 100, 800, 600)
window.setWindowTitle('ThFrame')
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()