| 24 | |
| 25 | |
| 26 | class SplitterHandle(QSplitterHandle): |
| 27 | clicked = pyqtSignal() |
| 28 | |
| 29 | def __init__(self, *args, **kwargs): |
| 30 | super(SplitterHandle, self).__init__(*args, **kwargs) |
| 31 | # 如果不设置这个,则鼠标只能在按下后移动才能响应mouseMoveEvent |
| 32 | self.setMouseTracking(True) |
| 33 | |
| 34 | def mousePressEvent(self, event): |
| 35 | super(SplitterHandle, self).mousePressEvent(event) |
| 36 | if event.pos().y() <= 24: |
| 37 | # 发送点击信号 |
| 38 | self.clicked.emit() |
| 39 | |
| 40 | def mouseMoveEvent(self, event): |
| 41 | """鼠标移动事件""" |
| 42 | # 当y坐标小于24时,也就是顶部的矩形框高度 |
| 43 | if event.pos().y() <= 24: |
| 44 | # 取消鼠标样式 |
| 45 | self.unsetCursor() |
| 46 | event.accept() |
| 47 | else: |
| 48 | # 设置默认的鼠标样式并可以移动 |
| 49 | self.setCursor(Qt.SplitHCursor if self.orientation() |
| 50 | == Qt.Horizontal else Qt.SplitVCursor) |
| 51 | super(SplitterHandle, self).mouseMoveEvent(event) |
| 52 | |
| 53 | def paintEvent(self, event): |
| 54 | # 绘制默认的样式 |
| 55 | super(SplitterHandle, self).paintEvent(event) |
| 56 | # 绘制顶部扩展按钮 |
| 57 | painter = QPainter(self) |
| 58 | painter.setRenderHint(QPainter.Antialiasing, True) |
| 59 | painter.setPen(Qt.red) |
| 60 | # 画矩形 |
| 61 | painter.drawRect(0, 0, self.width(), 24) |
| 62 | # 画三角形 |
| 63 | painter.setBrush(Qt.red) |
| 64 | painter.drawPolygon(QPolygonF([ |
| 65 | QPointF(0, (24 - 8) / 2), |
| 66 | QPointF(self.width() - 2, 24 / 2), |
| 67 | QPointF(0, (24 + 8) / 2) |
| 68 | ])) |
| 69 | |
| 70 | |
| 71 | class Splitter(QSplitter): |