diff --git a/QProxyStyle/Lib/TabCornerStyle.py b/QProxyStyle/Lib/TabCornerStyle.py new file mode 100644 index 0000000..f6df52b --- /dev/null +++ b/QProxyStyle/Lib/TabCornerStyle.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2021年06月23日 +@author: Irony +@site: https://pyqt.site , https://github.com/PyQt5 +@email: 892768447@qq.com +@file: TabCornerStyle +@description: +""" +from PyQt5.QtCore import QRect +from PyQt5.QtWidgets import QProxyStyle, QStyle + + +class TabCornerStyle(QProxyStyle): + + def subElementRect(self, element, option, widget): + try: + rect = super(TabCornerStyle, self).subElementRect(element, option, widget) + if element == QStyle.SE_TabWidgetRightCorner and rect.isValid(): + # 标签tab的矩形范围 + tab_rect = self.subElementRect(QStyle.SE_TabWidgetTabBar, option, widget) + # 内容面板的矩形范围 + panel_rect = self.subElementRect(QStyle.SE_TabWidgetTabPane, option, widget) + ext_height = 2 * self.pixelMetric(QStyle.PM_TabBarBaseHeight, option, widget) + # 修正过后填充的矩形范围 + cor_rect = QRect(tab_rect.x() + tab_rect.width() + ext_height, tab_rect.y() + ext_height, + panel_rect.width() - tab_rect.width() - 2 * ext_height, + tab_rect.height() - 2 * ext_height) + return cor_rect + return rect + except Exception as e: + print(e) + return QRect() diff --git a/QProxyStyle/README.md b/QProxyStyle/README.md index 7d9fe67..d64072b 100644 --- a/QProxyStyle/README.md +++ b/QProxyStyle/README.md @@ -2,6 +2,7 @@ - 目录 - [QTabWidget Tab文字方向](#1qtabwidget-tab文字方向) + - [QTabWidget 角落控件位置](#2qtabwidget-角落控件位置) ## 1、QTabWidget Tab文字方向 [运行 TabTextDirection.py](TabTextDirection.py) @@ -10,4 +11,15 @@ 2. `sizeFromContents` 转置size 3. `drawControl` 绘制文字 -![TabTextDirection](ScreenShot/TabTextDirection.png) \ No newline at end of file +![TabTextDirection](ScreenShot/TabTextDirection.png) + +## 2、QTabWidget 角落控件位置 +[运行 TabCornerWidget.py](TabCornerWidget.py) + +1. 通过 `app.setStyle(TabCornerStyle())` 设置代理样式 +2. `setCornerWidget` 设置自定义角落控件 + +原理是通过代理样式中对 `SE_TabWidgetRightCorner` 计算的结果进行校正,使得角落控件占满右边空白位置, +然后再配合自定义控件中使用 `QSpacerItem` 占据右边位置使得 + 号按钮居左,表现效果为 + 号按钮跟随标签的增加和减少 + +![TabCornerStyle](ScreenShot/TabCornerStyle.png) \ No newline at end of file diff --git a/QProxyStyle/ScreenShot/TabCornerStyle.png b/QProxyStyle/ScreenShot/TabCornerStyle.png new file mode 100644 index 0000000..4133f62 Binary files /dev/null and b/QProxyStyle/ScreenShot/TabCornerStyle.png differ diff --git a/QProxyStyle/TabCornerWidget.py b/QProxyStyle/TabCornerWidget.py new file mode 100644 index 0000000..d130788 --- /dev/null +++ b/QProxyStyle/TabCornerWidget.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2021年06月23日 +@author: Irony +@site: https://pyqt.site , https://github.com/PyQt5 +@email: 892768447@qq.com +@file: TabCornerWidget +@description: +""" +from PyQt5.QtCore import pyqtSignal, Qt +from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QSpacerItem, QSizePolicy, \ + QTabWidget + +from QProxyStyle.Lib.TabCornerStyle import TabCornerStyle + + +class TabCornerWidget(QWidget): + signalTabAdd = pyqtSignal() + + def __init__(self, *args, **kwargs): + super(TabCornerWidget, self).__init__(*args, **kwargs) + layout = QHBoxLayout(self) + layout.setContentsMargins(0, 0, 0, 0) + self.buttonAdd = QPushButton('+', self, toolTip='添加新标签页', clicked=self.signalTabAdd.emit) + layout.addWidget(self.buttonAdd) + layout.addItem(QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)) + + def resizeEvent(self, event): + super(TabCornerWidget, self).resizeEvent(event) + # 更新按钮高度 + if hasattr(self, 'buttonAdd'): + self.buttonAdd.setFixedSize(self.height(), self.height()) + + +if __name__ == '__main__': + import sys + + app = QApplication(sys.argv) + app.setStyle(TabCornerStyle()) + + tab1 = QTabWidget() + cor1 = TabCornerWidget(tab1) + cor1.signalTabAdd.connect(lambda: tab1.addTab(QWidget(tab1), 'tab' + str(tab1.count() + 1))) + tab1.setCornerWidget(cor1, Qt.TopRightCorner) + tab1.show() + + tab2 = QTabWidget() + tab2.setTabPosition(QTabWidget.South) # tab 标签方向 + cor2 = TabCornerWidget(tab2) + cor2.signalTabAdd.connect(lambda: tab2.addTab(QWidget(tab2), 'tab' + str(tab2.count() + 1))) + tab2.setCornerWidget(cor2, Qt.BottomRightCorner) + tab2.show() + + for i in range(10): + tab1.addTab(QWidget(tab1), 'tab' + str(i + 1)) + tab2.addTab(QWidget(tab1), 'tab' + str(i + 1)) + + sys.exit(app.exec_()) diff --git a/README.md b/README.md index acf26fc..05ebba7 100644 --- a/README.md +++ b/README.md @@ -222,6 +222,7 @@ https://pyqt.site 论坛是专门针对PyQt5学习和提升开设的网站,分 - [串口调试小助手](QSerialPort/SerialDebugAssistant.py) - [QProxyStyle](QProxyStyle) - [Tab文字方向](QProxyStyle/TabTextDirection.py) + - [Tab角落控件位置占满](QProxyStyle/TabCornerWidget.py) - [QMessageBox](QMessageBox) - [消息对话框倒计时关闭](QMessageBox/CountDownClose.py) - [自定义图标等](QMessageBox/CustomColorIcon.py)