forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RewriteHandle.py
112 lines (92 loc) · 3.38 KB
/
RewriteHandle.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年3月21日
@author: Irony
@site: https://pyqt5.com , https://github.com/892768447
@email: [email protected]
@file: Splitter
@description:
"""
import sys
from PyQt5.QtCore import Qt, QPointF, pyqtSignal
from PyQt5.QtGui import QPainter, QPolygonF
from PyQt5.QtWidgets import QTextEdit, QListWidget,\
QTreeWidget, QSplitter, QApplication, QMainWindow, QSplitterHandle
__Author__ = """By: Irony
QQ: 892768447
Email: [email protected]"""
__Copyright__ = "Copyright (c) 2018 Irony"
__Version__ = "Version 1.0"\
class SplitterHandle(QSplitterHandle):
clicked = pyqtSignal()
def __init__(self, *args, **kwargs):
super(SplitterHandle, self).__init__(*args, **kwargs)
# 如果不设置这个,则鼠标只能在按下后移动才能响应mouseMoveEvent
self.setMouseTracking(True)
def mousePressEvent(self, event):
super(SplitterHandle, self).mousePressEvent(event)
if event.pos().y() <= 24:
# 发送点击信号
self.clicked.emit()
def mouseMoveEvent(self, event):
"""鼠标移动事件"""
# 当y坐标小于24时,也就是顶部的矩形框高度
if event.pos().y() <= 24:
# 取消鼠标样式
self.unsetCursor()
event.accept()
else:
# 设置默认的鼠标样式并可以移动
self.setCursor(Qt.SplitHCursor if self.orientation()
== Qt.Horizontal else Qt.SplitVCursor)
super(SplitterHandle, self).mouseMoveEvent(event)
def paintEvent(self, event):
# 绘制默认的样式
super(SplitterHandle, self).paintEvent(event)
# 绘制顶部扩展按钮
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing, True)
painter.setPen(Qt.red)
# 画矩形
painter.drawRect(0, 0, self.width(), 24)
# 画三角形
painter.setBrush(Qt.red)
painter.drawPolygon(QPolygonF([
QPointF(0, (24 - 8) / 2),
QPointF(self.width() - 2, 24 / 2),
QPointF(0, (24 + 8) / 2)
]))
class Splitter(QSplitter):
def onClicked(self):
print('clicked')
def createHandle(self):
if self.count() == 1:
# 这里表示第一个分割条
handle = SplitterHandle(self.orientation(), self)
handle.clicked.connect(self.onClicked)
return handle
return super(Splitter, self).createHandle()
class SplitterWindow(QMainWindow):
def __init__(self, parent=None):
super(SplitterWindow, self).__init__(parent)
self.resize(400, 400)
self.setWindowTitle('PyQt Qsplitter')
textedit = QTextEdit('QTextEdit', self)
listwidget = QListWidget(self)
listwidget.addItem("This is a \nListWidget!")
treewidget = QTreeWidget()
treewidget.setHeaderLabels(['This', 'is', 'a', 'TreeWidgets!'])
splitter = Splitter(self)
splitter.setHandleWidth(8)
splitter.addWidget(textedit)
splitter.addWidget(listwidget)
splitter.addWidget(treewidget)
# Qt.Vertical 垂直 Qt.Horizontal 水平
splitter.setOrientation(Qt.Horizontal)
self.setCentralWidget(splitter)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = SplitterWindow()
main.show()
sys.exit(app.exec_())