分割窗口的分割条重写

This commit is contained in:
Irony 2018-03-21 20:57:06 +08:00
parent 328159606e
commit 96447c51cc
4 changed files with 124 additions and 0 deletions

View file

@ -20,6 +20,7 @@ encoding//tmp/\u7B80\u5355\u63D2\u4EF6/Widget.py=utf-8
encoding//\u4E0B\u62C9\u9009\u62E9\u8054\u52A8/ComboBox.py=utf-8 encoding//\u4E0B\u62C9\u9009\u62E9\u8054\u52A8/ComboBox.py=utf-8
encoding//\u4EBA\u8138\u63CF\u70B9\u68C0\u6D4B/OpencvWidget.py=utf-8 encoding//\u4EBA\u8138\u63CF\u70B9\u68C0\u6D4B/OpencvWidget.py=utf-8
encoding//\u5168\u5C40\u70ED\u952E/HotKey.py=utf-8 encoding//\u5168\u5C40\u70ED\u952E/HotKey.py=utf-8
encoding//\u5206\u5272\u7A97\u53E3\u7684\u5206\u5272\u6761\u91CD\u5199/Splitter.py=utf-8
encoding//\u53F3\u4E0B\u89D2\u5F39\u51FA\u6846/WindowNotify.py=utf-8 encoding//\u53F3\u4E0B\u89D2\u5F39\u51FA\u6846/WindowNotify.py=utf-8
encoding//\u56FE\u7247\u52A0\u8F7D/LoadImage.py=utf-8 encoding//\u56FE\u7247\u52A0\u8F7D/LoadImage.py=utf-8
encoding//\u56FE\u7247\u52A0\u8F7D/res_rc.py=utf-8 encoding//\u56FE\u7247\u52A0\u8F7D/res_rc.py=utf-8

View file

@ -0,0 +1,11 @@
# 分割窗口的分割条重绘
原理在于QSplitter在创建分割条的时候会调用createHandle函数
于是通过重新写createHandle返回自己的QSplitterHandle类
并通过QSplitterHandle的paintEvent实现绘制其它形状
重写mousePressEvent和mouseMoveEvent来实现鼠标的其它事件
## 截图
![截图](ScreenShot/1.gif)

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

View file

@ -0,0 +1,112 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年3月21日
@author: Irony
@site: https://github.com/892768447
@email: 892768447@qq.com
@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: 892768447@qq.com"""
__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_())